The Button widget

What does it do?

This is one of the most basic widgets, found in virtually all GUIs. The most common version is very simple: click on the button, and "something" happens. [Also see ToggleButton]

Creating an instance

Button(parent, text="", event=None, size=None, **kwargs)

While it's possible to create a Button with just a parent, you will usually want to add a label, and probably an event (although both of these can be (re)set later). A common idiom is

Button(parent, "some text", event=some_method)

Styles

Argument name Description
text Text (label) that appears on button. Can also be set later using SetLabel.
event Callable that will be hooked up to the OnClick event, so it will be called whenever the button is clicked.
size A tuple (width, height) indicating the default size of the button.
align Determines how the button's label will be aligned. Possible values are "left", "right", "top", "bottom", or "exact".
flat Indicates whether the button will be displayed as "flat". (Not supported on all platforms.)
exactfit Creates the button as small as possible, instead of making it of the standard size.

Events

Method name Called when...
OnClick ...button is clicked.

Common actions

Associating an event with the button

The easiest way to do this is to use the event parameter. In the following code, the OnButtonClick method is called whenever the button is clicked:

# button-1.py

from wax import *

class MainFrame(Frame):
    def Body(self):
        b = Button(self, "Click me", event=self.OnButtonClick)
        self.AddComponent(b, border=5, expand='both')
        self.Pack()
        self.Size = (100, 100)
    def OnButtonClick(self, event):
        print "Oh, ya!"

app = Application(MainFrame, title='button-1')
app.Run()

It is also possible to set the event dynamically on an existing button:

mybutton.OnClick = some_function

The OnClick method can also be overridden in subclasses:

class MyButton(Button):
    def OnClick(self, event):
        ...

(For more information, see the section on [Events].)

Getting/setting a button's text (label)

Use the GetLabel/SetLabel methods. Or, use the pseudo-properties associated with these methods. The following code reads the button's label, extracts information from it, and updates it.

# button-labels.py

from wax import *

class MainFrame(Frame):
    def Body(self):
        self.b = Button(self, "I was clicked 0 times", event=self.OnButtonClick)
        self.AddComponent(self.b, border=5, expand='both')
        self.Pack()
        self.Size = (100, 100)
    def OnButtonClick(self, event):
        # This is done better with a counter, but this way we demonstrate
        # the GetLabel and SetLabel methods:
        oldlabel = self.b.GetLabel()
        count = int(oldlabel.split()[3])
        newlabel = "I was clicked " + str(count + 1) + " times"
        self.b.SetLabel(newlabel)

app = Application(MainFrame, title='button-1')
app.Run()

Methods

...

Open issues

...