2016-11-10 13 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

DiceRollerが実際に.display属性を持っていないので、あなたはその

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

エラーが発生します。

あなたはそれはdieクラスが.display属性を持っているので、1を持っているが、die.__init__が呼び出されるとき、その属性が作成されます、そしてあなたがDiceRollerとそのメソッドをオーバーライドしたのでDiceRollerは、自動的に「die.__init__を呼び出すことはありません期待します独自の.__init__メソッド。

die.__init__DiceRoller.__init__に電話する場合は、これを行うことができます。推奨する方法は、super機能を使用することです。しかし、あなたは正しい引数でdie.__init__に電話するように注意する必要があります!

DiceRollerdieから継承する必要はありません。私は移動しましたDiceRollerの古い &の方法は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() 
関連する問題