为什么我会得到一个空白的tkinter窗口?

时间:2011-09-19 20:53:04

标签: python tkinter

所以当我运行此代码并单击按钮时:

from Tkinter import *
import thread
class App:
    def __init__(self, master):
        print master

        def creatnew():

            admin=Tk()
            lab=Label(admin,text='Workes')
            lab.pack()
            admin.minsize(width=250, height=250)
            admin.maxsize(width=250, height=250)
            admin.configure(bg='light green')
            admin.mainloop()
        def other():
            la=Label(master,text='other')
            la.pack()
            bu=Button(master,text='clicks',command=lambda: thread.start_new_thread(creatnew,()))
            bu.pack()
        other()

Admin = Tk()

Admin.minsize(width=650, height=500)
Admin.maxsize(width=650, height=500)
app = App(Admin)
Admin.mainloop()

我得到第二个tkinter窗口,但它是一个白色的空白屏幕,使两个程序都没有响应。 任何想法

1 个答案:

答案 0 :(得分:3)

不要使用线程。令人困惑的是Tkinter主循环。对于第二个窗口,创建一个Toplevel窗口。

您的代码经过最少的修改:

from Tkinter import *
# import thread # not needed

class App:
    def __init__(self, master):
        print master

        def creatnew(): # recommend making this an instance method

            admin=Toplevel() # changed Tk to Toplevel
            lab=Label(admin,text='Workes')
            lab.pack()
            admin.minsize(width=250, height=250)
            admin.maxsize(width=250, height=250)
            admin.configure(bg='light green')
            # admin.mainloop() # only call mainloop once for the entire app!
        def other(): # you don't need define this as a function
            la=Label(master,text='other')
            la.pack()
            bu=Button(master,text='clicks',command=creatnew) # removed lambda+thread
            bu.pack()
        other() # won't need this if code is not placed in function

Admin = Tk()

Admin.minsize(width=650, height=500)
Admin.maxsize(width=650, height=500)
app = App(Admin)
Admin.mainloop()