Tkinter Button command query

Paul A. Wilson scs2paw at leeds.ac.uk
Mon Apr 19 09:27:37 EDT 2004


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



More information about the Python-list mailing list