473,320 Members | 1,823 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,320 software developers and data experts.

Tkinter Button command query

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.

My button bar is implemented a Frame subclass, which takes the button
dictionary as an argument and displays the buttons on the screen:

class OptionsBar(Frame):
def __init__(self, buttonDict, parent=None)
Frame.__init__(self, parent)
parent.someFunction() # This works fine
for (name, command) in buttonDict.items():
Button(self, text=name, command=command).pack(side=TOP,
fill=BOTH)

and then another Frame subclass, e.g. MyWindow, uses the bar as
follows:

self.buttonDict = {'Button1':'someFunction',
'Button2':'otherFunction'}
...
myBar = OptionsBar(parent=self, buttonDict=self.buttonDict)
myBar.pack(side=RIGHT)
...

def someFunction():
do button stuff

My problem is that the Button instances aren't calling the correct
functions when pressed. Is there anyway I get Button to call its
parent frame's parent frame's functions? I've tried using

self.buttonDict = {'Button1':'parent.someFunction',
'Button2':'parent.otherFunction'}

but to no avail. Hope my explanations make sense.

Any suggestions?

Cheers
PAW
Jul 18 '05 #1
2 3200
Paul A. Wilson wrote:
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.

My button bar is implemented a Frame subclass, which takes the button
dictionary as an argument and displays the buttons on the screen:

class OptionsBar(Frame):
def __init__(self, buttonDict, parent=None)
Frame.__init__(self, parent)
parent.someFunction() # This works fine
for (name, command) in buttonDict.items():
Button(self, text=name, command=command).pack(side=TOP,
fill=BOTH)

and then another Frame subclass, e.g. MyWindow, uses the bar as
follows:

self.buttonDict = {'Button1':'someFunction',
'Button2':'otherFunction'}
...
myBar = OptionsBar(parent=self, buttonDict=self.buttonDict)
myBar.pack(side=RIGHT)
...

def someFunction():
do button stuff

My problem is that the Button instances aren't calling the correct
functions when pressed. Is there anyway I get Button to call its
parent frame's parent frame's functions? I've tried using

self.buttonDict = {'Button1':'parent.someFunction',
'Button2':'parent.otherFunction'}

but to no avail. Hope my explanations make sense.

Any suggestions?


Use the function identifier directly instead of a string to identify the
commands, e. g:

import Tkinter
class OptionsBar(Tkinter.Frame):
def __init__(self, buttonDict, parent=None):
Tkinter.Frame.__init__(self, parent)
parent.someMethod() # This works fine only if parent is not None
for (name, command) in buttonDict.items():
Tkinter.Button(self, text=name,
command=command).pack(side=Tkinter.TOP,
fill=Tkinter.BOTH)

def aFunction():
print "a function"

class MyWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, parent)
self.buttonDict = {'Button1': self.someMethod,
'Button2': aFunction}
#...
myBar = OptionsBar(parent=self, buttonDict=self.buttonDict)
myBar.pack(side=Tkinter.RIGHT)
#...

def someMethod(self):
print "some method"

root = Tkinter.Tk()
MyWindow(root).pack()
root.mainloop()

You can pass self.someMethod around as if it were a function. Python will
take care that the method is invoked with the instance denoted by self.

Note how I changed e. g. Button to Tkinter.Button. As you are already
creating reusable components you should also start to care about namespace
cluttering. If you are too lazy to type Tkinter in full beauty, use an
alias

import Tkinter as tk

and then tk.Button() etc.
Peter

PS: Do us - and yourself - the favour of using *4*-space indents. The more
people use it as a standard, the better you can tuck pieces of code from
different sources together.

Jul 18 '05 #2
Paul A. Wilson wrote:
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.

My button bar is implemented a Frame subclass, which takes the button
dictionary as an argument and displays the buttons on the screen:

class OptionsBar(Frame):
def __init__(self, buttonDict, parent=None)
Frame.__init__(self, parent)
parent.someFunction() # This works fine
for (name, command) in buttonDict.items():
Button(self, text=name, command=command).pack(side=TOP,
fill=BOTH)


Tkinter wants the functions, not the names of the functions. you can use
getattr() to get around this:

for (name, command) in buttonDict.items():
command = getattr(parent, command)
Button(self, text=name, command=command)...

(getattr(obj, "name") is the same as obj.name)

</F>


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Gregory | last post by:
Could someone help me out with these few lines of code: I would like to know why the Quit button in this application removes the buttons and causes "Quitting" to be printed, but does not close the...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
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...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
1
by: C D Wood | last post by:
To whom this may concern, Below is the source code, which demonstrates a problem I am having making a GUI for my python project work. 'table.txt' is a file that is read from the same folder....
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
2
by: skanemupp | last post by:
so my little calculator works perfectly now. just having some trouble with the layout. this whole tkinter-thing seems to be more tricky than it should be. how can i make the 4 column of buttons...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.