2011-09-19 6 views
0

donc quand je lance ce code et cliquez sur le bouton:Pourquoi une fenêtre tkinter vide?

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

i obtenir une deuxième fenêtre de tkinter mais son un écran vide blanc qui rend les deux programmes ne répondent. aucune idée

Répondre

3

N'utilisez pas de filetage. C'est déroutant le Tlointer Mainloop. Pour une seconde fenêtre, créez une fenêtre Toplevel.

Votre code avec des modifications minimales:

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

Vous ne devez appeler 'mainloop' fois. Vous ne l'appelez pas forveach toplevel, uniquement pour l'application dans son ensemble. –

+0

@Bryan Oakley: Correct! J'ai d'abord remarqué cela, mais j'ai oublié de le réparer. J'ai essayé de conserver autant de son code que possible. Fixé maintenant –

Questions connexes