Ethr
Ethr

Reputation: 461

How to open subwindows after each other in Tkinter?

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?

Upvotes: 0

Views: 116

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

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

Upvotes: 2

Related Questions