Tkinter Wiki   Widgets/Button UserPreferences
 
HelpContents FindPage Diffs Info Edit Subscribe XML Print View Up

1. 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.

2. Base Classes

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

3. 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.

4. Gotchas

Just giving a button default="active" doesn't create a binding for <Return> (the enter key). See [WWW]http://wiki.tcl.tk/534 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 
  2 
  3 
  4 
def command(func, *args, **kw):
    def _wrapper(*wargs):
        return func(*(wargs + args), **kw)
    return _wrapper

5. Parameter-passing

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 [WWW]Bob Greschke, for example.

6. References



PythonPowered