[Tkinter-discuss] What is wrong with this?

Stewart Midwinter stewart at midwinter.ca
Sun Jan 21 16:58:43 CET 2007

The problem with your test app is that in your button definition, you
didn't just provide the name of a method for the callback,you also
provided an argument for it.  That ensures that the callback will be
called as soon as the button is instantiated.

To get around this, one approach is to use a lambda function.  Another
approach is to create a separate function for each button and call it
without any arguments. Here are the final lines of your test app with
the 2nd approach:

         # Create the number and operator buttons
         self.topbuttonframe = Frame (self.mainframe, borderwidth = 2)
         self.topbuttonframe.pack (expand = 'yes', fill = 'both', side = TOP)
         self.button7 = Button (self.topbuttonframe, text = "7", width
= 2, command = self.buttonAction7)
         self.button7.pack (expand = 'yes', fill = 'both', side = LEFT)

     def buttonAction7 (self):
         action = '7'
         self.entry["state"] = NORMAL
         self.entry.insert (self.place, action)
         self.place = self.place + 1
         self.entry["state"] = DISABLED


More information about the Tkinter-discuss mailing list