0

I'm trying to build a simple photobooth type application with tkinter. I think I have what I want, except that tkinter isn't actually pausing (countdown to photo taken) when it should.

Here's a barebones example:

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()

When I run the application, it seems to work, but everything runs through too quick (no pausing). I'm new to tkinter, but the after() function should make the application loop pause, which it doesn't seem to do.

What am I doing wrong?

1

1 Answer 1

1

This should give you some idea of how you can think:

def take_photos(self,num_photos):
    for i in range(num_photos):
        delay = (i+1) * 1000
        self.master.after(delay, self.take_one_photo, i+1, num_photos)

def take_one_photo(self,n,m):
    # "Take photo" by displaying text
    self.display.config(text='Took {} of {} photos!'.format(n, m))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.