2011-09-19 79 views
0

所以,当我运行这段代码,然后单击按钮:为什么我会得到一个空白的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窗口,但它是一个白色的空白屏幕,使这两个程序没有响应。 任何想法

回答

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

您应该只调用'mainloop'一次。你不要把它称作整体,只适用于整个应用程序。 –

+0

@Bryan Oakley:对!我最初注意到,但忘了修复它。我试图尽可能保留他的代码。现在修复。 –

相关问题