Button refferences in Tkinter class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bellum
    New Member
    • Sep 2006
    • 23

    Button refferences in Tkinter class

    I'm rather new to both Tkinter and Classes, so whatever I'm doing wrong probably seems really stupid. Just bare with me, here. The reference to the buttons seems to disappear after the __init__ method, so when I try to call them and change them in another method, I get an attribute error.

    Code:
        def __init__(self):
            self.window = Tk()
            self.left = Frame(self.window, relief='sunken', border=1)
            self.middle = Frame(self.window, relief='sunken', border=1)
            self.right = Frame(self.window, relief='sunken', border=1)
            self.left.grid(column=0, row=0)
            self.middle.grid(column=2, row=0)
            self.right.grid(column=4, row=0)
    
            self.B1 = Button(self.left, text=' - ', command=self.changeButton(1))
            self.B2 = Button(self.middle, text=' - ', command=self.changeButton(2))
            self.B3 = Button(self.right, text=' - ', command=self.changeButton(3))
            self.B4 = Button(self.left, text=' - ', command=self.changeButton(4))
            self.B5 = Button(self.middle, text=' - ', command=self.changeButton(5))
            self.B6 = Button(self.right, text=' - ', command=self.changeButton(6))
            self.B7 = Button(self.left, text=' - ', command=self.changeButton(7))
            self.B8 = Button(self.middle, text=' - ', command=self.changeButton(8))
            self.B9 = Button(self.right, text=' - ', command=self.changeButton(9))
    
            self.B1.pack()
            self.B2.pack()
            self.B3.pack()
            self.B4.pack()
            self.B5.pack()
            self.B6.pack()
            self.B7.pack()
            self.B8.pack()
            self.B9.pack()
    One of the methods that raises the AttributeError:

    Code:
        def gameClear(self):
            self.B1['text'] = ' - '
            self.B2['text'] = ' - '
            self.B3['text'] = ' - '
            self.B4['text'] = ' - '
            self.B5['text'] = ' - '
            self.B6['text'] = ' - '
            self.B7['text'] = ' - '
            self.B8['text'] = ' - '
            self.B9['text'] = ' - '
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I prefer to call the config() method of Tkinter objects. Try:[CODE=python]#
    def gameClear(self) :
    self.B1.config( text=' - ')[/CODE]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      I prefer to call the config() method of Tkinter objects. Try:[CODE=python]#
      def gameClear(self) :
      self.B1.config( text=' - ')[/CODE]
      Here is a discussion on sub-classing and instanciating Tk() and its subordinates.

      Comment

      Working...