2011-09-19 154 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:對!我最初注意到,但忘了修復它。我試圖儘可能保留他的代碼。現在修復。 –

相關問題