Я пытаюсь создать простое приложение типа фотостенда с помощью tkinter. Я думаю, что у меня есть то, что я хочу, за исключением того, что tkinter на самом деле не делает паузу (обратный отсчет до сделанного снимка), когда должен.
Вот простой пример:
import tkinter as tk
class Photobooth:
def __init__(self, master):
self.master = master
master.title('Photo Booth')
self.seconds = 3
self.secs = 3
self.num_photos = 0
self.display = tk.Label(master, height=20, width=60, textvariable="")
self.display.config(text='Photobooth!')
self.display.grid(row=0, column=0, columnspan=1)
self.three_photo_button = tk.Button(master,
bg='Blue',
activebackground='Dark Blue',
text='Take 3 Photos',
width=60,
height=8,
command=lambda: self.take_photos(3))
self.three_photo_button.grid(row=1, column=0)
def tick(self):
# Need a dummy function to pause with.
pass
def take_photos(self, num_photos):
self.secs = self.seconds
# Cycle through each photo
for n in range(num_photos):
# Cycle through countdown timer
for s in range(self.seconds):
# Display seconds left
self.display.config(text='{}'.format(self.secs))
if self.secs == 0:
# "Take photo" by displaying text
self.display.config(text='Took {} of {} photos!'.format(n, num_photos))
# Wait a fraction of a second, then continue.
self.master.after(100, self.tick)
# Except if we are at our photo limit, then show: 'Done'.
if n == (num_photos - 1):
self.display.config(text='Done!')
self.master.after(100, self.tick)
else:
# Decrease timer
self.secs -= 1
# Wait one second
self.master.after(1000, self.tick)
if __name__ == '__main__':
root = tk.Tk()
my_timer = Photobooth(root)
root.mainloop()
Когда я запускаю приложение, оно, кажется, работает, но все проходит слишком быстро (без пауз). Я новичок в tkinter, но функция after()
должна сделать паузу цикла приложения, что, похоже, не делает.
Что я делаю не так?