[英]Tkinter Not Updating Label Text
我有一個用Python編寫的程序,單擊按鈕將滾動3個骰子,並隨着滾動更新GUI。
這是代碼:
import random
import Tkinter
class Die():
def roll(self):
value = random.randint(1,6)
self.display.config(text=value)
def __init__(self, display):
self.value = 0
self.display = Tkinter.Label(display, text=self.value, relief='ridge', borderwidth=5, bg='white')
self.display.pack(side='left')
class DiceRoller:
def rollDice(self):
Die.roll
def __init__(self):
window = Tkinter.Tk()
window.title("Die Roller")
frame = Tkinter.Frame(window)
self.dice = [Die(frame), Die(frame), Die(frame)]
rollAgain = Tkinter.Button(window, text = "Roll Again", command=self.rollDice)
frame.pack()
rollAgain.pack(side='bottom')
window.mainloop()
program = DiceRoller()
除了函數rollDice()
不會更新框架上的骰子標簽這一事實外,所有與代碼有關的內容均有效。 我單擊我的rollAgain
按鈕,但文本沒有更新。
有人可以幫我嗎? 謝謝。
我通過以下操作解決了我的問題:
def rollDice(self):
for die in self.dice:
die.roll()
通過遍歷我的骰子,我可以滾動每個骰子。 這樣可以避免創建新的Die對象,而該對象顯然會創建4個骰子,而我只需要3個。
因此,對於任何偶然發現此問題的人,都有解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.