Remove buttons for a few seconds in Python/Tkinter? - python

I'm writing a program where the user will make a selection based on a target image. I'm trying to get the program to remove the selection buttons and wait 2 seconds after updating the target image before the selection choices are re-presented. The code that I have seems to "disable" the clicked button for 2 seconds, but does not remove either button.
from tkinter import *
import random
root = Tk()
root.geometry("500x500")
def click_b(event):
btn_b.pack_forget()
btn_c.pack_forget()
new_a()
root.update()
root.after(2000, show_btns())
def click_c(event):
btn_b.pack_forget()
btn_c.pack_forget()
new_a()
root.update()
root.after(2000, show_btns())
def new_a():
k = random.randrange(1, 3)
if k == 1:
btn_a.configure(image=a1)
elif k == 2:
btn_a.configure(image=a2)
def show_btns():
btn_b.pack(side=LEFT)
btn_c.pack(side=RIGHT)
a1 = PhotoImage(file="A1.gif")
a2 = PhotoImage(file="A2.gif")
orange = PhotoImage(file="orange_btn.gif")
green = PhotoImage(file="yellowgreen_btn.gif")
btn_a = Button(root, image=a1)
btn_a.pack()
btn_b = Button(root, image=orange)
btn_b.bind('<Button-1>', click_b)
btn_b.pack(side=LEFT)
btn_c = Button(root, image=green)
btn_c.bind('<Button-1>', click_c)
btn_c.pack(side=RIGHT)
root.mainloop()

the issues is in your after() methods.
You need to remove the brackets for the show_btns function call or else tkinter will not run this command properly. If you have a function with no arguments you leave off the () portion.
If you do have arguments then you will need to either provide those arguments in the after statement IE after(2000, some_func, arg1, arg2) or use lambda to create a one off function to do the work like after(2000, lambda: some_func(arg1, arg2)). lambda can be more complicated but this is the basic concept.
change:
after(2000, show_btns())
To:
after(2000, show_btns)
As long as your paths to your images work fine the below code should work as intended.
from tkinter import *
import random
root = Tk()
root.geometry("500x500")
def click_b(event):
btn_b.pack_forget()
btn_c.pack_forget()
new_a()
root.update()
root.after(2000, show_btns)
def click_c(event):
btn_b.pack_forget()
btn_c.pack_forget()
new_a()
root.update()
root.after(2000, show_btns)
def new_a():
k = random.randrange(1, 3)
if k == 1:
btn_a.configure(image=a1)
elif k == 2:
btn_a.configure(image=a2)
def show_btns():
btn_b.pack(side=LEFT)
btn_c.pack(side=RIGHT)
a1 = PhotoImage(file="A1.gif")
a2 = PhotoImage(file="A2.gif")
orange = PhotoImage(file="orange_btn.gif")
green = PhotoImage(file="yellowgreen_btn.gif")
btn_a = Button(root, image=a1)
btn_a.pack()
btn_b = Button(root, image=orange)
btn_b.bind('<Button-1>', click_b)
btn_b.pack(side=LEFT)
btn_c = Button(root, image=green)
btn_c.bind('<Button-1>', click_c)
btn_c.pack(side=RIGHT)
root.mainloop()

Related

Python tkinter image can't change immediately?

My code:
from tkinter import *
import random
from PIL import ImageTk, Image
import time
class App():
def __init__(self, root, dice_image):
self.dice_label = Label(root, image=dice_image)
self.dice_label.pack()
self.button = Button(root, text="Roll", font=("Fixedsys", 25) , width=20, height=20, bg="red" , command=rolling)
self.button.pack(pady=70)
I want to make the image change fast to make it look like rolling. But I don't know why it change nothing before the for loop end.
def rolling():
global dices
for i in range(1, 10 + 1):
time.sleep(0.3)
random.shuffle(dices)
app.dice_label.configure(image=dices[0])
if __name__ == '__main__':
root = Tk()
root.geometry("800x600")
# Var
dice_image = ImageTk.PhotoImage(Image.open("Dices.jpg"))
dice1 = ImageTk.PhotoImage(Image.open("Dice1.jpg"))
dice2 = ImageTk.PhotoImage(Image.open("Dice2.jpg"))
dice3 = ImageTk.PhotoImage(Image.open("Dice3.jpg"))
dice4 = ImageTk.PhotoImage(Image.open("Dice4.jpg"))
dice5 = ImageTk.PhotoImage(Image.open("Dice5.jpg"))
dice6 = ImageTk.PhotoImage(Image.open("Dice6.jpg"))
dices = [dice1, dice2, dice3, dice4, dice5, dice6]
app = App(root, dice_image)
root.mainloop()
Tkinter and sleep() are not friends. The problem is that Tkinter functions inside of a mainloop and sleep pauses that loop until all the sleep time is over.
This will always freeze your application.
Try using after() and a refactored function instead.
I have not tested this but I believe something like this should fix your problem or at least get rid of the problem that sleep() will cause.
I have changed your button command to send a call to the function using lambda so that the call wont go out at runtime but only when pressing the button.
from tkinter import *
import random
from PIL import ImageTk, Image
class App():
def __init__(self, root, dice_image):
self.dice_label = Label(root, image=dice_image)
self.dice_label.pack()
self.button = Button(root, text="Roll", font=("Fixedsys", 25),
width=20, height=20, bg="red" ,
command=lambda: rolling(1,11))
self.button.pack(pady=70)
I have updated your rolling function to handly a timed loop.
root.after(300, lambda: rolling(stop, counter)) will call the function it is in only if the counter has not finished counting down and only once every 0.3 seconds. Again lambda is used here to make sure the call to rolling does not happen at runtime.
def rolling(s, e, start=False):
global dices
counter = 0
stop = 0
if start:
counter = s
stop = e
if counter > stop:
random.shuffle(dices)
app.dice_label.configure(image=dices[0])
counter -= 1
root.after(300, lambda: rolling(stop, counter))
I have also updated this portion to use a loop so you don't have to repeat yourself when crating a list of images.
if __name__ == '__main__':
root = Tk()
root.geometry("800x600")
dice_image = ImageTk.PhotoImage(Image.open("Dices.jpg"))
dices = []
for i in range(6):
dices.append(ImageTk.PhotoImage(Image.open(f"Dice{i+1}.jpg")))
app = App(root, dice_image)
root.mainloop()

Is there a way to create a second window that connects to the parent window like a dropdown menu

I'm trying to make it so that new information shows in in a new window, but I want the new window to be connected to the parent window, even when the parent window is clicked the new window should still show up similar to how a dropdown menu works. I'm also planning on having some of the new windows have treeviews later on.
from tkinter import *
win = Tk()
win.geometry("500x500+0+0")
def button_function ():
win2 = Toplevel()
label = Label(win2,text='dropdown', width=7)
label.pack()
win2.geometry(f"+{win.winfo_x()}+{win.winfo_y()+30}")
button = Button(win, command=lambda: button_function (), width=12)
button.pack()
win.mainloop()
Ok so with a little bit of googling I came across this post: tkinter-detecting-a-window-drag-event
In that post they show how you can keep track of when the window has moved.
By taking that code and making some small changes we can use the dragging() and stop_drag() functions to move the top level window back to where it was set to relative to the main window.
That said this will only work in this case. You will need to write something more dynamic to track any new windows you want so they are placed properly and on top of that you will probably want to build this in a class so you do not have to manage global variables.
With a combination of this tracking function and using lift() to bring the window up we get closer to what you are asking to do.
That said you will probably want remove the tool bar at the top of the root window to be more clean. I would also focus on using a dictionary or list to keep track of open and closed windows and their locations to make the dynamic part of this easier.
import tkinter as tk
win = tk.Tk()
win.geometry("500x500+0+0")
win2 = None
drag_id = ''
def dragging(event):
global drag_id
if event.widget is win:
if drag_id == '':
print('start drag')
else:
win.after_cancel(drag_id)
print('dragging')
drag_id = win.after(100, stop_drag)
if win2 is not None:
win2.lift()
win2.geometry(f"+{win.winfo_x()}+{win.winfo_y() + 30}")
def stop_drag():
global drag_id, win2, win
print('stop drag')
drag_id = ''
if win2 is not None:
win2.lift()
win2.geometry(f"+{win.winfo_x()}+{win.winfo_y() + 30}")
win.bind('<Configure>', dragging)
def button_function():
global win2
win2 = tk.Toplevel()
label = tk.Label(win2, text='drop down', width=7)
label.pack()
win2.geometry(f"+{win.winfo_x()}+{win.winfo_y()+30}")
tk.Button(win, command=button_function, width=12).pack()
win.mainloop()
EDIT:
Ok so I took some time to write this up in a class so you could see how it could be done. I have also added some level of dynamic building of the buttons and pop up windows.
We use a combination of lists and lambdas to perform a little bit of tracking and in the end we pull off exactly what you were asking for.
let me know if you have any questions.
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('500x500')
self.pop_up_list = []
self.drag_id = ''
self.button_notes = ['Some notes for new window', 'some other notes for new window', 'bacon that is all!']
self.bind('<Configure>', self.dragging)
for ndex, value in enumerate(self.button_notes):
print(ndex)
btn = tk.Button(self, text=f'Button {ndex+1}')
btn.config(command=lambda b=btn, i=ndex: self.toggle_button_pop_ups(i, b))
btn.grid(row=ndex, column=0, padx=5, pady=5)
self.pop_up_list.append([value, 0, None, btn])
def dragging(self, event):
if event.widget is self:
if self.drag_id == '':
pass
else:
self.after_cancel(self.drag_id)
self.drag_id = self.after(100, self.stop_drag)
for p in self.pop_up_list:
if p[1] == 1:
p[2].lift()
p[2].geometry(f"+{p[3].winfo_rootx() + 65}+{p[3].winfo_rooty()}")
def stop_drag(self):
self.drag_id = ''
for p in self.pop_up_list:
if p[1] == 1:
p[2].lift()
p[2].geometry(f"+{p[3].winfo_rootx() + 65}+{p[3].winfo_rooty()}")
def toggle_button_pop_ups(self, ndex, btn):
p = self.pop_up_list
if p[ndex][1] == 0:
p[ndex][1] = 1
p[ndex][2] = tk.Toplevel(self)
p[ndex][2].overrideredirect(1)
tk.Label(p[ndex][2], text=self.pop_up_list[ndex][0]).pack()
p[ndex][2].geometry(f"+{btn.winfo_rootx() + 65}+{btn.winfo_rooty()}")
else:
p[ndex][1] = 0
p[ndex][2].destroy()
p[ndex][2] = None
if __name__ == '__main__':
Main().mainloop()

Check if a Tkinter button remains clicked

I want to make 2 buttons (+ and -) that can change the volume. But if I want to increase the volume more, I'd like to can keep the button pressed. Any ideas how to check if a button remains pressed by the user?
def volumeUp():
currentVolume = m.getvolume()[0]
volumeSlider.set(currentVolume + 5)
def volumeDown():
currentVolume = m.getvolume()[0]
volumeSlider.set(currentVolume - 5)
volumeDownButton = Button(win, text = "-", font = myFont, command = volumeDown, height = 1, width = 1)
volumeDownButton.pack(side = BOTTOM)
volumeUpButton = Button(win, text = "+", font = myFont, command = volumeUp, height = 1, width = 1)
volumeUpButton.pack(side = BOTTOM)
What you can do is make the Button press fire a function that alters the volume and then schedules itself to be run again after a certain amount of time (e.g. 100 ms). Then when the Button is released, you can cancel the scheduled repeat of the function that alters the volume to break the loop.
I've altered your code a bit to make an example:
from tkinter import *
win = Tk()
def volumeUpPress(e=None):
global up_after
currentVolume = volumeSlider.get()
volumeSlider.set(currentVolume + 2)
up_after = win.after(100, volumeUpPress)
def volumeUpRelease(e=None):
global up_after
win.after_cancel(up_after)
def volumeDownPress(e=None):
global down_after
currentVolume = volumeSlider.get()
volumeSlider.set(currentVolume - 2)
down_after = win.after(100, volumeDownPress)
def volumeDownRelease(e=None):
global down_after
win.after_cancel(down_after)
volumeSlider = Scale(win, from_=0, to=100, orient=HORIZONTAL)
volumeSlider.pack()
volumeDownButton = Button(win, text = "-", height = 1, width = 1)
volumeDownButton.pack(side = BOTTOM)
volumeDownButton.bind("<Button-1>", volumeDownPress)
volumeDownButton.bind("<ButtonRelease-1>", volumeDownRelease)
volumeUpButton = Button(win, text = "+", height = 1, width = 1)
volumeUpButton.pack(side = BOTTOM)
volumeUpButton.bind("<Button-1>", volumeUpPress)
volumeUpButton.bind("<ButtonRelease-1>", volumeUpRelease)
win.mainloop()
Things to note:
I didn't use the Button's command since that doesn't give you the flexibility of knowing when the Button is released. Instead I made two binds, one for when the Button is pressed, one for when it is released.
I use the after method to schedule a new call to the same function after a set number of milliseconds, I save a reference to this scheduled function in a global variabel, to be able to use it again in the release function to cancel it with after_cancel
.bind calls functions with an event object, while after calls a function without arguments, because both call the same function, I made it so that the function can be called both with and without argument (e=None)
An alternative to fhdrsdg's answer that also uses after would be to measure the state value of the Button and detect whether it is currently active, to do this we bind a function to the Button which checks the state and then increments a value if the state is active before using after to call the function again after a short delay:
from tkinter import *
class App():
def __init__(self, root):
self.root = root
self.button = Button(self.root, text="Increment")
self.value = 0
self.button.pack()
self.button.bind("<Button-1>", lambda x: self.root.after(10, self.increment))
def increment(self, *args):
if self.button["state"] == "active":
self.value += 1
print(self.value)
self.root.after(10, self.increment)
root = Tk()
App(root)
root.mainloop()

Closing TK window after button is pressed (regardless of button) while the button still performs its action

I'm trying to have my Tk window perform a function when a button is pressed, and they automatically close itself. I assume I need some sort of destroy() function inside of the action function, but I don't know how to word it.
Here is what I am trying to do
import pandas as pd
from tkinter import *
import numpy as np
from functools import partial
fake data
test = pd.DataFrame(columns = ["id", 'sent', "O1", "O2", "O3", "O4"])
results = []
for i in range(5):
test.loc[i,:] = [i,"this is test "+ str(i), .2, .5, .1, .1]
levels = [["Baby"], ["Dinos"], ["bad"], ["Spoons"]]
###
This is the action I want it to take. It needs to record what was pressed, then delete the window afterwards. I think this is where my destroy() function needs to go, but I'm not sure how to word it.
def Add_results(option):
results.append(option)
My window maker
def Window_maker(sent, choices):
root = Tk()
topFrame = Frame(root)
topFrame.pack()
botFrame = Frame(root)
botFrame.pack()
label = Label(topFrame, text =sent)
label.pack()
indi= 0
button1 = Button(botFrame, text = choices[0], command = lambda: Add_results(option = choices[0]))
button1.pack()
button2 = Button(botFrame, text = choices[1], command = lambda: Add_results(option = choices[1]))
button2.pack()
root.mainloop()
return(results)
The implementation
for i in range(test.shape[0]):
index = get_params(test.iloc[i, 2:])
choices = [levels[x] for x in index.values]
pred = Window_maker(test.iloc[i,1], choices)
I found a fix.
I change Add_results to:
def Add_results(option):
results.append(option)
root.quit()
And it worked!

Launch command in Tkinter based on selected radio button?

I would like to change the function and text of a button based on which radio-button is selected. Right now what's happening is that both commands are run at the same time as soon as the application is launched rather than it being based upon which radio-button is selected.
import tkinter as tk
import time
## Time variables for the Pomodoro
pomo = 60 * 25 ## 60 seconds times number of minutes
btime = 60 * 5 ## 60
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label = tk.Label(self, text="25:00", width=10, font="Helvetica 20")
self.label.pack()
self.remaining = 0
self.button = tk.Button(self)
self.button.pack()
pomocommand = self.button.configure(text="Pomodoro", state=tk.NORMAL, command= lambda: self.pomodoro(pomo)) #Switch back to the pomodoro timer
breakcommand = self.button.configure(text="Break", state=tk.NORMAL, command= lambda: self.breaktime(btime)) #Switch to the break timer
countercommand = self.button.configure(text="Counter", state=tk.NORMAL, command= print('cheese'))
self.radvar = tk.IntVar()
self.radvar.set('1')
self.radio = tk.Radiobutton(self, text="Pomodoro", variable = self.radvar, value=1, indicatoron=0, command = pomocommand)
self.radio.pack(anchor=tk.W)
self.radio = tk.Radiobutton(self, text="Counter", variable = self.radvar, value=2, indicatoron=0, command = countercommand)
self.radio.pack(side=tk.LEFT)
def pomodoro(self, remaining = None):
self.button.configure(state=tk.DISABLED)
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="Time's up!")
breakcommand
else:
self.label.configure(text= time.strftime('%M:%S', time.gmtime(self.remaining))) #Integer to 'Minutes' and 'Seconds'
self.remaining = self.remaining - 1
self.after(1000, self.pomodoro)
def breaktime(self, remaining = None):
self.button.configure(state=tk.DISABLED)
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.label.configure(text="Time's up!")
pomocommand
else:
self.label.configure(text= time.strftime('%M:%S', time.gmtime(self.remaining))) #Integer to 'Minutes' and 'Seconds'
self.remaining = self.remaining - 1
self.after(1000, self.breaktime)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
What you are doing is calling the function self.button.configure(...) instead of passing the function itself. Here is a small example:
def test(a):
return a+4
callback_1 = test(2) # callback_1 will be (2+4) = 6, because you called the function. Notice the parens ()
callback_2 = test # callback_2 will be the function test, you did not call it
# So, you can do:
callback_2(some_value) # will return (some_value + 4), here you called it
Basically what is happening is that you are using the first example, so the function is called in __init__, and what it is supposed to do is done there. What you want is something similar to the second, because Tkinter wants something to call (a function or any callable). So pomocommand and break should be functions not the result of calling a function. (You can try to do print pomocommand and see that it is not a function.)
The solution is either to create a new function, or use lambdas. Here:
def pomocommand(self):
self.button.configure(text="Pomodoro", state=tk.NORMAL, command= lambda: self.pomodoro(pomo)) #Switch back to the pomodoro timer
# and in your __init__ method:
def __init__(self):
# ...
self.radio = tk.Radiobutton(self, text="Pomodoro", variable = self.radvar, value=1, indicatoron=0, command = self.pomocommand)
# ...
And you do the same for the other button. Using a lambda here is not recommended because it is a long statement (the self.button.configure part) so your code will not be readable. But I see you are using lambdas, so you might get the idea.
As a side note, be careful when using lambda like you do. Here pomo is a global variable, but if it is not, you might get into trouble. See this question. (Right now it is alright, so you can just ignore this :D).

Categories

Resources