Tkinter.Button - create and manipulate button widgets

A button is a widget that displays a textual string, bitmap or image. If text is displayed, it must all be in a single font, but it can occupy multiple lines on the screen (if it contains newlines or if wrapping occurs because of the wraplength= option) and one of the characters may optionally be underlined using the underline option. It can display itself in either of three different ways, according to the state. to disable a button set state = DISABLED. To enable set state = NORMAL. Tk sets state = ACTIVE when mouse is over a non-DISABLED button) option; it can be made to appear raised, sunken, or flat; and it can be made to flash. When a user invokes the button (by pressing mouse button 1 with the cursor over the button), then the command specified in the command= option is invoked. If the command is a callable object, it is called with no arguments. If it is a string, it is executed as Tcl code.

Base Classes

../Widget, ../BaseWidget, ../Pack, ../Place, ../Grid, ../Misc

Default Bindings

Tk automatically creates class bindings for buttons that give them default behavior:

  1. A button activates whenever the mouse passes over it and deactivates whenever the mouse leaves the button. Under windows, this binding is only active when mouse button 1 has been pressed over the button.
  2. A button's relief is changed to sunken whenever mouse button 1 is pressed over the button, and the relief is restored to its original value when button 1 is later released.
  3. If mouse button 1 is pressed over a button and later released over the button, the button is invoked. However, if the mouse is not over the button when button 1 is released, then no invocation occurs.
  4. When a button has the input focus, the space key causes the button to be invoked.

If the button's state is disabled then none of the above actions occur: the button is completely non-responsive. The behavior of buttons can be changed by defining new bindings for individual widgets or by redefining the class bindings.


Just giving a button default="active" doesn't create a binding for <Return> (the enter key). See and search for "-default active" for some Tcl code related to this.

The standard Button widget does not allow parameters to be passed by the command method. This can be circumvented by re-writing the method as below:

   1 def command(func, *args, **kw):
   2     def _wrapper(*wargs):
   3         return func(*(wargs + args), **kw)
   4     return _wrapper


Newcomers to Tkinter frequently ask about passing parameters through a button selection. The wrapper command above, supplied by Guilherme Polo, can be used as ... It's a clever alternative to Tkinter's more traditional style as expressed by Bob Greschke, for example. Here and here are two more variations that allow parameters to be passed to buttons [and explain %W some time]. Also, Francesco Bochicchio once noted in private correspondence, that, "[w]ith modern python, an alternative to use [of] lambda or a home-made callable is to use functools.partial :

  b = Button( command = functools.partial( mycallback, myarg ))


Compound Buttons

See: ExampleCompoundButton


tkinter: Widgets/Button (last edited 2014-03-16 21:49:51 by sam)