2011-09-19 3 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 창을 만듭니다. 최소한의 수정을

귀하의 코드 : 당신은`한 번 mainloop` 호출해야

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

. 당신은 전체 앱에 대해서만 최고 수준으로 호출하지 않습니다. –

+0

@ 브라이언 오클리 : 맞다! 나는 처음에 그것을 알아 차렸지만 그것을 고치지 않았다. 가능한 많은 코드를 보존하려고했습니다. 이제 해결되었습니다. –

관련 문제