# Like java SimpleButton2, but using OOP # Weird import for compatibility across Python 2 and Python 3 try: import tkinter except ImportError: import Tkinter as tkinter # Make our own button class, subclass of standard one class MyButton (tkinter.Button): # Args = label you want, button's parent widget or window def __init__(self, label, parent): # Call superclass' constructor as usual tkinter.Button.__init__ (self, parent) # Set our stuff self["text"] = label self["command"] = self._myCallback self._count = 0 self.pack() # Our callback is a method, not a global function def _myCallback (self): self._count += 1 print (self["text"] + " was pushed " + str(self._count) + " times") # Create main frame top = tkinter.Tk() # Put 2 buttons in it, using our class b1 = MyButton ("Push me", top) b2 = MyButton ("Push me also", top) # Start the main GUI loop top.mainloop()