Reputation: 366
I am making a application that needs 2 windows. The first one, I do all the standard stuff like
root = tk.Tk()
...code...
root.mainloop()
But for my second window, I only call
root = tk.Tk()
and it works. If I do
root = tk.Tk()
...code...
root.mainloop()
it still works. Out of pure curiosity, why?
Code:
import tkinter as tk
from tkinter.messagebox import showerror
from time import sleep
class DecompilingChecker(object):
def __init__(self):
self.master = tk.Tk()
self.master.withdraw()
self.master.title("Test Program: Update")
def check(self, file, directory):
self.master.update()
self.master.deiconify()
class TestProgram(object):
pass
class GUI(object):
def __init__(self, master):
self.master = master
self.master.title("Test Program")
tk.Text(self.master).grid(row=0, column=0)
self.decompilingchecker = DecompilingChecker()
self.decompilingchecker.check(None, None)
class Bridge(object):
def __init__(self):
self.root = tk.Tk()
GUI(self.root)
def run(self):
self.root.mainloop()
if __name__ == "__main__":
try:
bridge = Bridge()
bridge.run()
except Exception as e:
showerror("Test Program: ERROR!", "An error has occurred!\n{0}".format(str(e)))
``
Upvotes: 1
Views: 29
Reputation: 36662
You should not call tk.Tk()
more than once. For additional windows, use tk.Toplevel()
. Calling update
is only necessary in some rare cases, your GUI is most times better off relying on the mainloop
for the updates.
import tkinter as tk
class DecompilingChecker(tk.Toplevel):
def __init__(self, master):
super().__init__(master)
self.title("DecompilingChecker Toplevel")
def check(self):
print('checking')
class Bridge(tk.Toplevel):
def __init__(self, master):
super().__init__(master)
self.title("Bridge Toplevel")
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("GUI window")
self.bridge = Bridge(self)
self.d_checker = DecompilingChecker(self)
self.d_checker.check()
if __name__ == "__main__":
GUI().mainloop()
Upvotes: 2