Command Buttons and Event Handlers

Each command button has associated with it a method that is triggered when the user selects that button.  breezypythongui provides a default event handler method that does nothing.  However, an application normally supplies its own event handler method as an argument when a button is added to the window.  The programmer must then define this method in the application windowÕs class.  Here is the code for associating an event handler method with the command button in the tax calculator program:

def __init__(self):

    .

    .

    # Add the command button

    self.addButton(text = "Compute", row = 3, column = 0,

                   columnspan = 2, command = self.computeTax)

 

# The event handler method for the button

def computeTax(self):

    """Obtains the data from the input fields and uses

    them to compute the tax, which is sent to the

    output field."""

    income = self.incomeField.getNumber()

    numDependents = self.depField.getNumber()

    exemptionAmount = self.exeField.getNumber()

    tax = (income - numDependents * exemptionAmount) * .15

    self.taxField.setNumber(tax)

 

If an application needs more than one command button, each button is added to the window in the __init__ method.  The programmer should then define a separate event handler method for each button.