I want to dynamically create Tkinter windows on my screen. I understand that I should only have one mainloop(). I use the threading module to make mainloop execute in a seperate thread, so it will not block the script.
How do I create more Tkinter windows after I executed mainloop?
Please take a look at my code:
from Tkinter import *
import threading
import time
class box:
def __init__(self, pos):
self.master = Tk()
self.master.geometry(pos)
self.canvas = Canvas(self.master, width=50, height=50, highlightthickness=0 )
self.canvas.pack()
self.rect = self.canvas.create_rectangle(0, 0, 50, 50, fill="red", outline="red")
self.text = self.canvas.create_text(25, 24, text="99",fill="white", font=("calibri", 24, "bold"))
def changeFill(self, color):
self.canvas.itemconfig(self.rect, fill=color, outline=color) # change color
class box_manager(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.boxes = {}
self.add_box(1, "50x50+300+300")
self.add_box(2, "50x50+100+100")
def add_box(self, num, pos):
self.boxes[num] = box(pos)
def run(self):
mainloop()
tk = box_manager()
tk.start()
# How do I dynamically add new tkinter windows? the line below makes python.exe crash.
tk.add_box(3, "50x50+200+200")
Update after Joel’s comment, still doesn’t work:
from Tkinter import *
import threading
import time
class MyCustomWindow(Toplevel):
def __init__(self):
Toplevel.__init__(self)
#setup goes here
self.geometry("50x50+100+100")
self.canvas = Canvas(self, width=50, height=50, highlightthickness=0 )
self.canvas.pack()
class App(Tk):
def CreateFirst(self):
self.anotherWindow = MyCustomWindow()
def CreateSecond(self):
self.secondWindow = MyCustomWindow()
class SecondWindow(threading.Thread):
#after 2 seconds create a second window, python.exe crashes
def run(self):
time.sleep(2)
tk.CreateSecond()
SecondWindow().start()
tk = App()
tk.CreateFirst()
mainloop()
You don’t. That’s not how Tkinter is designed to work. You should always call mainloop exactly once, and from the main thread.