pythonursina

How do I get the return value from a function?


How is it possible to get a return value from a function in a button?

What I would like to do is:

from ursina import *


app = Ursina()

def on_click():
    #Some code
    return #Some value

b = Button(on_click = on_click)

def input(key):
    if key == "left mouse down":
        print(b.returnvalue)

app.run()

but this obviously does not work.


Solution

  • As detailed in this answer, you can either declare a global variable before on_click(), and set that variable inside on_click() using the global keyword before the name of the variable.

    It would be preferable however to create a class for your button, and create a member variable that you set inside an on_click() method and can later access with a getter method.

    If you need a refresher on OOP concepts, I would recommend this article, or this video.