The state variable can store and retain a single value while the project is running, but if you want to store multiple values persistently, you will need to pass them from the Python code inside the Live Python block to generic controls. The values can then be passed back to the Python code when they are needed.
Notes:
To store and reuse values from Python code
total=0
# Save value 1 button as a string
if inputs[0].changed_get():
inputs[4].string_set("25")
# Save value 2 button as a string
if inputs[1].changed_get():
inputs[5].string_set("50")
# Show add integer values of saved strings and display
if inputs[2].changed_get():
total = int(inputs[4].string_get()) + int(inputs[5].string_get())
message.string_set(total)
# Reset displayed values
if inputs[3].changed_get():
inputs[4].string_set("")
inputs[5].string_set("")
message.string_set("")
The value 50 is saved in the second string control.
The final value 75 is calculated using the saved values, then it is displayed on the control surface of the Live Python block.
If you want to run the sequence again, click the Reset button, and refer to step 12.
See also Controlling the execution of Python code |