Forum Archive

Calculator - predefined number

BryanPatrick

Is there anyone who could help me modify the original calculator example so that you can pass an argument to it from url to predefine the first number for an equation?

dgelessus

This can be done relatively easily. At the top of the script, add import sys to be able to access the runtime/URL arguments. Then add the following code at the bottom:

if len(sys.argv) > 1:
    v['label1'].text = sys.argv[1]

Basically it checks if there are any runtime arguments (argument 0 is always the script file name) and if there are, it uses the first argument as the display text. Since the calculator pulls the values directly from the text label and doesn't use any other internal variables to store the equation, no other changes are necessary. You can even pass an entire equation, as long as it doesn't contain any spaces.

BryanPatrick

Thank you very much!