2016-03-04 227 views
-1

當函數c()被調用時,StringVar.get()方法返回一個空值。但是,當我只調用new_db()函數時,它工作得很好。 我真的不明白這個問題。有人可以向我解釋嗎?Tkinter中的Python StringVar()。get()返回空白值

#modules 
import os 
from Tkinter import * 

chance=3 
def cr(): 
    print data.get() 

#new_db 

def new_db(): 
    global data 
    m.destroy() 
    new=Tk() 
    data=StringVar() 


    Entry(new,font='BRITANIC 16',textvariable=data).grid(column=1,row=2) 
    Button(new,text='Create New Database',command=cr).place(x=175,y=75) 
    new.geometry('500x100+400+250') 
    new.mainloop() 

def c(): 
    global m 
    m=Tk() 
    Button(m,text='erferf',command=new_db).pack() 
    m.mainloop() 
c() 
+0

在Python 3中,您的代碼(使用'Tkinter'重命名並且'print'作爲函數調用)適用於我。 – Blckknght

回答

1

看這個答案When do I need to call mainloop in a Tkinter application?。它告訴主循環()必須只被調用一次。

另外,當點擊按鈕執行new_db()時,Tk對象m應該仍然存在。

對於你試圖完成的任務,你應該只創建一次Tk(),並且只調用一次mainloop()。然後你應該放置代碼來隱藏/顯示相應的小部件。看看In Tkinter is there any way to make a widget not visible?瞭解如何顯示/隱藏小部件。

+0

謝謝,它只在調用一次mainloop時有效 – Ruth