Events


An event is just calling a function - you want an event to be generated every time the user does something, such as clicking a button, dragging a scale, or pressing a key...

The Button has an event automatically linked to it - whenever you press it, a function gets called. The other widgets don't.

Types of Event


appJar currently has four basic types of event you can register:

Change & Submit Functions

These do similar things, so probably shouldn't both exist, but have evolved from a single .set XXX Function() which is now deprecated.

WARNING - it's possible to generate a RuntimeError. If you've got two widgets changing the same variable, say a Scale and a SpinBox, and you want a change in one widget to cause an update in the other, you might inadvertently end up stuck in a recursive loop, until the stack overflows.

In this case, make sure you set the optional parameter callFunction = False when you call the set XXX Function() of a widget.

from appJar import gui

def songChanged(rb):
    print(app.getRadioButton(rb))

def reset(btn):
    # set back to the default, but don't call the change function
    app.setRadioButton("song", "Killer Queen", callFunction=False)

app=gui()
app.addRadioButton("song", "Killer Queen")
app.addRadioButton("song", "Paradise City")
app.setRadioButtonChangeFunction("song", songChanged)
app.addButton("Reset", reset)
app.go()

Advertisement why?


Over Functions


Set functions to call whenever the mouse enters (goes over) or leaves the specified widget.

    from appJar import gui

    def enter(wdgt): 
        print("IN", wdgt)
    def leave(wdgt):
        print("OUT", wdgt)

    app=gui()
    app.addLabel("l1", "Testing...")
    app.setLabelOverFunction("l1", [enter, leave])
    app.go()

Drag Functions

Set functions to call when the mouse button is clicked and dragged on a Label, then released.

Registering Other Event Types

It's possible to register any of the standard event types with appJar widgets

app.getEntryWidget("widget_name").bind("<FocusOut>", function_name, add="+")

Binding Keys


As well as changing widgets, we sometimes want keys to trigger events.
The classic example is the <Enter> key, we often want to be able to hit the <Enter> key to submit a form...

You may also want to bind other keys to events.
See here for a detailed list of the Event Formats.

from appJar import gui
def keyPress(key):
    if key == "<Up>":
        app.increaseFont()
    elif key == "<Down>":
        app.decreaseFont()
    elif key == "<F1>":
        app.setFont(12)

app = gui("Button Demo")
app.addLabel("title", "Press the arrow keys to change the font")
app.bindKey("<Up>", keyPress)
app.bindKey("<Down>", keyPress)
app.bindKey("<F1>", keyPress)
app.go()

Stopping the GUI


Usually the user just presses the close icon to stop the GUI.
However, you might want to let them do it in other ways - maybe by pressing a button...

To stop the GUI, simply call app.stop()

If you want to add a feature to confirm the user really wants to exit, or to save some data, then you'll need a stop function.

def checkStop():
    return app.yesNoBox("Confirm Exit", "Are you sure you want to exit the application?")

app.setStopFunction(checkStop)