jarvisteach / appJar

Simple Tkinter GUIs in Python
http://appJar.info
Other
615 stars 68 forks source link

Bringing parameters into function on button click #548

Closed BadgerHobbs closed 5 years ago

BadgerHobbs commented 5 years ago

Context / Sample Code

I'm trying to carry some values on the button click into the function that the button calls.

This code should bring the following values into the function UpdateLocalConfig : LocalConfig (from variable), 0, "south_line_point_motor_01","FORWARD" into the function when button clicked.

def UpdateLocalConfig(LocalConfig,Position,Item,Value):

    LocalConfig[Position][1] = Value
    print("Update: \n"+Item,"=",Value)
    UpdateConfig(LocalConfig)
    return;
app.addNamedButton("01","01G",UpdateLocalConfig(LocalConfig,0,"south_line_point_motor_01","FORWARD"),0)

Actual Behaviour

Nothing Happens, as function runs with no parameters, and does nothing.

BadgerHobbs commented 5 years ago

I've created the following workaround, but it is not clean.

I put the information (apart from LocalConfig) into a string as the name of the button, with hashtags to separate the values. This is broken up into its parts to be used. The LocalConfig is set to a global variable so that the function can access it.

#Function to update the local config gile
def UpdateLocalConfig(ButtonID):

    ButtonIDArray = []
    ButtonIDArray = ButtonID.split("#")

    Position = int(ButtonIDArray[0])
    Item = ButtonIDArray[1]
    Value = ButtonIDArray[2]

    #Change specific position item value in array
    LocalConfig[Position][1] = Value
    #print what was updated (for debugging)
    print("Update: \n"+Item,"=",Value)
    #Run local config into update config function
    #Updates config file
    UpdateConfig(LocalConfig)
    return;

Here is the split up information as the name of the button. Since i was using named buttons before and i wanted this functionality still, each button is renamed with the correct title after creation. This enables the buttons to be functional while having the correct names in the GUI.

app.addButton("0#SW111#reverse#01",UpdateLocalConfig,0)

app.setButton("0#SW111#reverse#01","SW111")
Mr-Jarvis commented 5 years ago

It's not possible to set additional parameters for a button.

I struggle to see a use case for this - if you hard code the parameters, then you can hard code them into the actual function instead, using the button ID to determine which set of parameters to use.

If you want them to be dynamic, again this can be done in the function.

In your first sample of code, putting brackets after the function name causes the function to be called the moment the button is created, rather than when it is clicked, so that's not really valid syntax anyway.

In your second example, why not just have a dictionary of config data, and use the button ID as the key?

globalConfig = {
    'SW111': [0,'SW111', 'reverse', '01']
}

def UpdateLocalConfig(id):
    config = globalConfig[id]
    print(config)

app.addButton('SW111', UpdateLocalConfig)