下面的代碼在Tkinter的標籤插件的顯示文本就是我從Tkinter的進口參考 *我似乎更改基於條件
root = Tk()
#Variables
answer = "Enter Answer"
data = ""
#Functions
def function():
data = e.get()
while data == "":
if data == 5:
answer = "Correct"
if data != 5:
answer = "Incorrect"
print(answer)
top = Label(root, text = "Test")
top.pack()
e = Entry(root)
e.pack()
e.focus_set()
b = Button(root, text = "Enter", command = function)
b.pack()
check = Label(root, text = answer)
check.pack()
mainloop()
不能更新標籤控件(名稱「檢查')。 我希望能夠基於檢查條件來更新它,但我無法使其工作。 我把「打印(答案)」線檢查的變量,但我得到的錯誤:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "G:/Portable Apps/Portable Python 3.2.5.1/Documents/TEST.py", line 22, in function
print(answer)
UnboundLocalError: local variable 'answer' referenced before assignment
這當我運行該程序時,鍵入一個值,然後選擇ENTER鍵。