2016-11-10 39 views
1

這是我的理解是,類DiceRoller應該從類die被繼承,但是我每次運行時出現錯誤:爲什麼我的Tkinter標籤不更新?

self.display.config(text = str(self.value)) 
AttributeError: 'DiceRoller' object has no attribute 'display' 

self.value值更新,但Tkinter的標籤是沒有的。

import Tkinter 
import random 

class die(object): 
    def __init__(self,value,display): 
     self.value = random.randint(1,6) 
     self.display = Tkinter.Label(display, 
      text = str(self.value), 
      font = ('Garamond', 56), 
      bg = 'white', 
      relief = 'ridge', 
      borderwidth = 5) 
     self.display.pack(side = 'left') 

class DiceRoller(die): 
    def __init__(self): 
     self.gameWin = Tkinter.Tk() 
     self.gameWin.title('Dice Roller') 
     self.gameFrame = Tkinter.Frame(self.gameWin) 
     self.dice = [] 
     self.Row1 = Tkinter.Frame(self.gameWin) 
     for i in range(1,4): 
      self.dice.append(die(i,self.Row1)) 
     self.topFrame = Tkinter.Frame(self.gameWin) 
     self.rollBtn = Tkinter.Button(self.topFrame, 
      text = 'Roll Again', 
      command = self.rollDice, 
      font = ('Garamond', 56)) 
     self.rollBtn.pack(side = 'bottom') 

     self.gameFrame.pack() 
     self.Row1.pack() 
     self.topFrame.pack() 
     self.gameWin.mainloop() 
    def rollDice(self): 
     self.value = random.randint(1,6) 
     print self.value #to show value is in fact changing 
     self.display.config(text = str(self.value)) 

varName = DiceRoller() 
+0

你正在設置'text = str(self.value)' - 這是一次性的,它不是某種綁定,它會在'self.value'更改時更新。設置'textvariable = self.value',一個直接引用。 – jonrsharpe

+0

[使python/tkinter標籤小部件更新?]的可能重複?(http://stackoverflow.com/questions/1918005/making-python-tkinter-label-widget-update) – jonrsharpe

回答

1

你得到

AttributeError: 'DiceRoller' object has no attribute 'display' 

錯誤,因爲DiceRoller實際上並沒有一個.display屬性。

您可以期望它有一個因爲die類有一個.display屬性,但屬性被當die.__init__被調用創建的,DiceRoller不會自動調用die.__init__,因爲你已經覆蓋該方法與DiceRoller「自己的.__init__方法。

如果你想在DiceRoller.__init__裏面調用die.__init__你可以這樣做,建議的方法是使用super函數。但是你必須小心地用正確的參數呼叫die.__init__

但是,DiceRoller不需要從die繼承。我已將DiceRoller的舊rollDice方法移至die &爲DiceRoller創建了新的rollDice方法。所以當按下'Roll Again'按鈕時,所有骰子DiceRoller.dice都會滾動。

import Tkinter as tk 
import random 

class die(object): 
    def __init__(self, value, display): 
     self.value = random.randint(1,6) 
     self.display = tk.Label(display, 
      text = str(self.value), 
      font = ('Garamond', 56), 
      bg = 'white', 
      relief = 'ridge', 
      borderwidth = 5) 
     self.display.pack(side = 'left') 

    def rollDice(self): 
     self.value = random.randint(1,6) 
     print self.value #to show value is in fact changing 
     self.display.config(text = str(self.value)) 

class DiceRoller(object): 
    def __init__(self): 
     self.gameWin = tk.Tk() 
     self.gameWin.title('Dice Roller') 
     self.gameFrame = tk.Frame(self.gameWin) 
     self.dice = [] 
     self.Row1 = tk.Frame(self.gameWin) 
     for i in range(1,4): 
      self.dice.append(die(i, self.Row1)) 
     self.topFrame = tk.Frame(self.gameWin) 
     self.rollBtn = tk.Button(self.topFrame, 
      text = 'Roll Again', 
      command = self.rollDice, 
      font = ('Garamond', 56)) 
     self.rollBtn.pack(side = 'bottom') 

     self.gameFrame.pack() 
     self.Row1.pack() 
     self.topFrame.pack() 
     self.gameWin.mainloop() 

    def rollDice(self): 
     for die in self.dice: 
      die.rollDice() 

DiceRoller()