Search code examples
pythonappjar

appJar - ListBox/OptionBox widget call a function on select


I recently started playing with appJar python module and I got stuck using its widgets, namely ListBox http://appjar.info/pythonWidgets/#listbox and OptionBox http://appjar.info/pythonWidgets/#optionbox.

I am not able to find out, how to call function when item in ListBox or OptionBox is selected. I found this syntax, but I cannot find where to define the function I want to call:

.selectListItem(title, item, callFunction=True)
.changeOptionBox(title, newOptions, index, callFunction=False)

This is the code, which I have so far. As you can see, I am able to print out selected values, but by calling method on button click.

from appJar import gui

def press(btn):
    if btn == "Cancel":
        app.stop()
    elif btn == "Show":
        list_select()
    else:
        print('not defined yet')

def list_select():
    app.infoBox("Info", "You selected " + app.getOptionBox("optionbox") + "\nBrowsing " + app.getListBox("list")[0])

app = gui("Database Editor", "500x500")
app.addOptionBox("optionbox", ["a", "b", "c", "d"])
app.addListBox("list", ["one", "two", "three", "four"])

app.addButtons(["Show", "Cancel"], press)
app.go()

Is any of you aware, how can I print out these values directly on the select?


Solution

  • I've figured it out! Indeed the ChangeFunction can be triggered through the Event function http://appjar.info/pythonEvents/#types-of-event (as I posted in comments) .

    I used these two events to do this thing:

    app.setOptionBoxChangeFunction("optionbox", opt_changed)
    app.setListBoxChangeFunction("list", lst_changed)
    

    The whole working code here:

    from appJar import gui
    
    def opt_changed(opt):
        print(app.getOptionBox("optionbox"))
    
    def lst_changed(lst):
        print(app.getListBox("list")[0])
    
    def press(btn):
        if btn == "Cancel":
            app.stop()
        elif btn == "Show":
            list_select()
        else:
            print('not defined yet')
    
    def list_select():
        app.infoBox("Info", "You selected " + app.getOptionBox("optionbox") + "\nBrowsing " + app.getListBox("list")[0])
    
    app = gui("Database Editor", "500x500")
    app.addOptionBox("optionbox", ["a", "b", "c", "d"])
    app.addListBox("list", ["one", "two", "three", "four"])
    
    app.setOptionBoxChangeFunction("optionbox", opt_changed)
    app.setListBoxChangeFunction("list", lst_changed)
    
    app.addButtons(["Show", "Cancel"], press)
    app.go()