Working with global variables

Hello, This is Mario. I have some doubts about the use of Forms and Modules. I need a global variable to use along with my Form1 page. ¿Could someone help me?

Why don’t you just make a new property instead? If you really need a global variable, the python global keyword is available in Anvil:

global my_global
my_global = 'some data'

But the global variable must be defined out of the function? I mean, in Python, imI define de def xx() function and inside y define the global variables
In this case, with Anvil, every time that I want to use a global variable should I define it?

Global my_global
My_global = some data

Global another_data
another_data = other data

Is it correct?? Thank you very much

with Global variables – esp. those that get marked with the global keyword – it doesn’t matter where in the file you define it since it is global. You shouldn’t have to define it each time, but you might need to use the global keyword if the global value isn’t being used.

Thank you very much for your support. I tried it and it works. Nevertheless, I think that I should change the method. I hace already programes the code in python tkinter. But is difficult to transform it into a web application. I tried with Anvil, but maybe I should use flask or django. Thank you very much

Have a nice weekend

Mario

An option that you can only use if you have a main form that loads other forms in its container, that is if your main form is created once and exists forever, is to create the global variables on the main form and access them with get_open_form().global_variable.

A more elegant option is to create a module and call it Globals where you define all your global variables. Then you import that module on all the forms and you have access to all the variables defined in Globals.

5 Likes