如何在mainloop()之后创建新的Tkinter窗口?

时间:2013-01-15 11:11:45

标签: python tkinter

我想在屏幕上动态创建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")

在Joel的评论之后更新,仍然不起作用:

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

2 个答案:

答案 0 :(得分:4)

  

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

你没有。这不是Tkinter的设计工作方式。你应该总是一次调用mainloop,并从主线程调用。

答案 1 :(得分:3)

其他(非root)窗口只是Toplevel窗口小部件。您只需将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()

修改

当然, <{em>} <{em>}