1
我試圖製作一個程序,在GUI中顯示標籤'HI'
,只需在同一GUI中單擊按鈕'CLICK'
後。單擊按鈕後在相同的gui中顯示標籤
我的代碼:
import Tkinter as tki
class App(object):
def __init__(self,root):
self.root = root
txt_frm = tki.Frame(self.root, width=900, height=900)
txt_frm.pack(fill="both", expand=True)
button3 = tki.Button(txt_frm,text="CLICK", command = self.retrieve_inpu)
button3.grid(column=0,row=2)
def retrieve_inpu(self):
label = tki.Label(txt_frm,text='HI')
label.grid(column=0,row=3)
root = tki.Tk()
app = App(root)
root.mainloop()
,但我得到的錯誤是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:/Python27/teste.py", line 14, in retrieve_inpu
label = tki.Label(txt_frm,text='HI')
NameError: global name 'txt_frm' is not defined
請幫我點擊按鈕'CLICK'
後顯示在同一GUI標籤'HI'
。
可能ü請重新鍵入整個代碼?因爲我仍然得到相同的錯誤.. –
這是因爲您也需要在創建按鈕時使用'self.txt_frm':'button3 = tki.Button(self.txt_frm,...) '。 'txt_frm'現在是'App'的一個實例屬性,所以它總是必須通過'self'來訪問。 – iCodez