Tkinter Button command query
Fredrik Lundh
fredrik at pythonware.com
Mon Apr 19 10:59:32 EDT 2004
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>
More information about the Python-list
mailing list