473,378 Members | 1,469 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,378 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 3209
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...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.