所以我使用Python 3.4和tkinter。標籤不斷出現
當我再次調用一個包含標籤的函數時,標籤會一直出現在窗口中,但之前的標籤不會消失?
一旦函數被調用,我怎樣從GUI窗口中刪除任何打印標籤,然後顯示新標籤?
下面是代碼: -
#def prestart():
#here I check if number of match is okay, if not, user is redirected to setting else, I call start()
def start():
#CPU Choice
cpu_choice = Label(historyframe, text = "CPU Choosed: {}".format(dict['cpu_choice']))
#Played Match
#played_num_of_match = Label(scoreframe, text = "Number of Matches Played: {}".format(int(dict['match_played'])))
#Display Status
status_disp = Label(scoreframe, text = "Current Status: {}".format(dict['status']))
if(int(dict['match_played']) < int(dict['num_of_match'])):
playframe.grid(row = 1, column = 0)
historyframe.grid(row = 2, column = 1)
status_disp.pack(fill=X)
elif(int(dict['match_played']) == int(dict['num_of_match'])):
playframe.grid(row = 1, column = 0)
historyframe.grid(row = 2, column = 1)
status_disp.pack(fill=X)
cp = dict['cpu_point']
up = dict['user_point']
result(cp, up)
cpu_choice.pack(fill = X)
scoreframe.grid(row = 2, column = 0)
此功能只更新顯示!
def send_value(x):
#Here I run logic of game and change value of key in dictionary and call start() at end of change.
現在,選擇按鈕沒有任何定義,因爲它們不需要再次被再次調用n。我只是讓播放幀消失而出現! 下面是代碼爲他們: -
#Display Question
question = Label(playframe, text = "Rock? Paper? Scissor?")
#Rock
rock = Button(playframe, text = "Rock!", command = lambda: send_value("ROCK"))
#Paper
paper = Button(playframe, text = "Paper!", command = lambda: send_value("PAPER"))
#Scissor
scissor = Button(playframe, text = "Scissor!", command = lambda: send_value("SCISSOR"))
所以,當用戶點擊搖滾/紙/剪刀,我只是在字典改變鍵值!但是如果我將標籤保留在功能之外,它不會自動更新!
其他一切都很完美。我現在開始讓代碼更清潔。
爲什麼不更新現有標籤中的文本?另外,顯示一些代碼。 –
你只是試圖更新標籤?你爲什麼在一個函數中創建它? – maccartm
請向我們展示一個說明問題的MCVE。 –