0

我想在我的屏幕上动态创建 Tkinter 窗口。我知道我应该只有一个 mainloop()。我使用线程模块使 mainloop 在单独的线程中执行,因此它不会阻塞脚本。

执行 mainloop 后如何创建更多 Tkinter 窗口?

请看一下我的代码:

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

乔尔发表评论后更新,仍然不起作用:

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

2 回答 2

5

执行 mainloop 后如何创建更多 Tkinter 窗口?

你没有。这不是 Tkinter 设计的工作方式。您应该始终只从主线程调用一次 mainloop。

于 2013-01-15T12:07:11.470 回答
3

其他(非根)窗口只是Toplevel小部件。您只需 subclass Toplevel,并从您的主类中调用它:

class MyCustomWindow(tkinter.Toplevel):
    def __init__(self):
        tkinter.Toplevel.__init__(self)
        #setup goes here


class App(tkinter.Tk):

    def someCallback(self):
        self.anotherWindow = MyCustomWindow()

编辑

你当然不必Toplevel化,你可以直接使用它。

于 2013-01-15T12:13:25.047 回答