Skip to content
Advertisement

Kivy: How to access global variables in .kv file

I’m fairly new to kivy and need some help with accessing a global variable in kivy. The idea of this demo code is to be able to update a global variable via a class method. These methods would, in practice, be buttons that a user presses that update the variable. I want to know how I can access this new variable depending on which function is used. IMPORTANT NOTE: These methods cannot be accessed via ClassA().CA2().x as in practice they would have too many parameters to navigate. Is there an alternative method that allows me to access the updated variable without having to access the method directly?

Python File:

JavaScript

Kivy File:

JavaScript

Advertisement

Answer

root refers to ClassA, not main.py. Therefore x must be a property of the class, not a local variable. Here is the code that does what you are looking for.

JavaScript

#Note: You must name the kivy file my.kv for it to work since from the code I refer to it as class MyApp(App):

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement