python - Why aren't my Tkinter Labels updating? -


it's understanding class diceroller should inheriting class die, every time run error:

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

the value of self.value updating, tkinter label not.

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)         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 in fact changing         self.display.config(text = str(self.value))  varname = diceroller() 

you

attributeerror: 'diceroller' object has no attribute 'display'  

error because diceroller doesn't have .display attribute.

you may expect have 1 because die class has .display attribute, attribute gets created when die.__init__ gets called, , diceroller won't automatically call die.__init__ because you've overridden method diceroller's own .__init__ method.

if want call die.__init__ inside diceroller.__init__ can that, suggested way use super function. have careful call die.__init__ correct arguments!

however, there's no need diceroller inherit die. i've moved diceroller's old rolldice method die & created new rolldice method diceroller. when 'roll again' button pressed dice in diceroller.dice rolled.

import tkinter 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 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)         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):         die in self.dice:             die.rolldice()  diceroller() 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -