0

I'm trying to open few windows one after the other in tkinter. They are closing itself after set amount of time. Creating first window breaks my loop (it doesn't even reach print instruction). What am I doing wrong? Here is my code:

import tkinter as tk

class Subwindow:

    def __init__(self, time):
        self.time = time
        self.window = tk.Toplevel()
        self.window.geometry("300x300+1000+200")
        self.window.wm_attributes("-topmost", 1)
        tk.Label(self.window, text=time).pack(anchor=tk.CENTER)
        self.update_time()
        self.window.mainloop()

    def update_time(self):
        if self.time:
            self.window.title(str(self.time))
            self.time -= 1
            self.window.after(1000, self.update_time)
        else:
            self.window.destroy()

class Window:

    def __init__(self):
        self.window = tk.Tk()
        self.initialize()
        self.window.mainloop()

    def initialize(self):
        buttons = tk.Frame(self.window)
        tk.Button(self.window, text="Start", width=5, command=self.start).pack(in_=buttons, side="left")
        tk.Button(self.window, text="Exit", width=5, command=self.close).pack(in_=buttons, side="left")
        buttons.place(relx=0.97, rely=0.95, anchor=tk.SE)

    def close(self):
        self.window.destroy()
        quit()

    def start(self):
        for x in [5,10,15]:
            sub = Subwindow(x)
            print(x)

Window()

Explain me please how can I fix it to get them opening one by one?

1 Answer 1

2

You should not call mainloop more than once. Remove the call to mainloop inside Subwindow.__init__.

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.