473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Button refferences in Tkinter class

23
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.

Expand|Select|Wrap|Line Numbers
  1.     def __init__(self):
  2.         self.window = Tk()
  3.         self.left = Frame(self.window, relief='sunken', border=1)
  4.         self.middle = Frame(self.window, relief='sunken', border=1)
  5.         self.right = Frame(self.window, relief='sunken', border=1)
  6.         self.left.grid(column=0, row=0)
  7.         self.middle.grid(column=2, row=0)
  8.         self.right.grid(column=4, row=0)
  9.  
  10.         self.B1 = Button(self.left, text=' - ', command=self.changeButton(1))
  11.         self.B2 = Button(self.middle, text=' - ', command=self.changeButton(2))
  12.         self.B3 = Button(self.right, text=' - ', command=self.changeButton(3))
  13.         self.B4 = Button(self.left, text=' - ', command=self.changeButton(4))
  14.         self.B5 = Button(self.middle, text=' - ', command=self.changeButton(5))
  15.         self.B6 = Button(self.right, text=' - ', command=self.changeButton(6))
  16.         self.B7 = Button(self.left, text=' - ', command=self.changeButton(7))
  17.         self.B8 = Button(self.middle, text=' - ', command=self.changeButton(8))
  18.         self.B9 = Button(self.right, text=' - ', command=self.changeButton(9))
  19.  
  20.         self.B1.pack()
  21.         self.B2.pack()
  22.         self.B3.pack()
  23.         self.B4.pack()
  24.         self.B5.pack()
  25.         self.B6.pack()
  26.         self.B7.pack()
  27.         self.B8.pack()
  28.         self.B9.pack()
  29.  
One of the methods that raises the AttributeError:

Expand|Select|Wrap|Line Numbers
  1.     def gameClear(self):
  2.         self.B1['text'] = ' - '
  3.         self.B2['text'] = ' - '
  4.         self.B3['text'] = ' - '
  5.         self.B4['text'] = ' - '
  6.         self.B5['text'] = ' - '
  7.         self.B6['text'] = ' - '
  8.         self.B7['text'] = ' - '
  9.         self.B8['text'] = ' - '
  10.         self.B9['text'] = ' - '
  11.  
Jun 23 '07 #1
2 1668
bartonc
6,596 Expert 4TB
I prefer to call the config() method of Tkinter objects. Try:
Expand|Select|Wrap|Line Numbers
  1. #
  2. def gameClear(self):
  3.         self.B1.config(text=' - ')
Jun 23 '07 #2
bartonc
6,596 Expert 4TB
I prefer to call the config() method of Tkinter objects. Try:
Expand|Select|Wrap|Line Numbers
  1. #
  2. def gameClear(self):
  3.         self.B1.config(text=' - ')
Here is a discussion on sub-classing and instanciating Tk() and its subordinates.
Jun 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
8
by: dakman | last post by:
Recently, I have been needing to do this alot and I can never find a way around it, the main reason I want to do this is because for example in the application I am making right now, it creates a...
2
by: Fuzzyman | last post by:
Hello all, I have some Tkinter buttons that display images. I would like to change these to 'active' images when the mouse is over the button. I see that the button widget can take an...
8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
5
by: vagrantbrad | last post by:
I've created a short test program that uses tkFileDialog.askdirectory to help the user input a path into a tk entry widget. The problem I'm having is that when I run the code as listed below, the...
1
by: akineko | last post by:
Hello everyone, I'm trying to implement a virtual instrument, which has buttons and displays, using Tkinter+Pmw. One of items on the virtual instrument is a round button. This is imitating a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.