Python – Set the global variable Kivy

Set the global variable Kivy… here is a solution to the problem.

Set the global variable Kivy

I’m using a screen manager to manage multiple different screens. One screen has two buttons that both lead to the other, but depending on which button is pressed, I want one label to display different text on the second screen. Here is my code :

.py

MY_GLOBAL = "test"

class ChooseProgScreen(Screen):
    global MY_GLOBAL
    def setTitle(self, newTitle):
        MY_GLOBAL = newTitle
        print(MY_GLOBAL)

class SwitchScreen(Screen):
    global MY_GLOBAL
    def getTitle(self):
        return MY_GLOBAL

class ScreenManagement(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        presentation = Builder.load_file("kivy.kv")
        return presentation

.kv

ScreenManagement:
    transition: FadeTransition()
    HomeScreen:
    ChooseProgScreen:
    SwitchScreen:
    NewProgScreen:

<ChooseProgScreen>:
    name: "chooseprog"
    FloatLayout:
        Button:
            text: "test1"
            on_release: 
                root.setTitle("test1")
                app.root.current = "switch"
            color: 1,1,1,1
            font_size: 25
            size_hint: 0.15,0.15
            pos_hint: {"center_x":.1, "center_y":.9}
        Button:
            text: "test2"
            on_release:
                root.setTitle("test2")
                app.root.current = "switch"
            color: 1,1,1,1
            font_size: 25
            size_hint: 0.15,0.15
            pos_hint: {"center_x":.3, "center_y":.9}

<SwitchScreen>:
    name: "switch"
    FloatLayout:
        Label:
            text: root.getTitle()
            pos_hint: {"center_x":.1, "center_y":.1}
            font_size: 25

In ChooseProgScreen in .kv, when the button is released, I call a method from the .py file, set the global variable to the new screen and print it. When you press one of the buttons, the print section works fine, the global variable is printed as a new string, but the SwitchScreen label still shows

up

"test"

And not

"test1" or "test2"

I

think global variables might be a bad way to do this, but I don’t know how to do this using the screen manager and kivy language. It would be appreciated if someone could help with the proper use of global variables, or suggest a better way to do this.

Edit

The problem is that the screen only updates on the first load. I added an update method to SwitchScreen:

def update(self):
    self.ids.switchtitle.text = self.getTitle()

and updated SwitchScreen: in .kv files

<SwitchScreen>:
    on_enter:
        root.update()
    Label:
        id: switchtitle
        text: root.getTitle()
        pos_hint: {"center_x":.1, "center_y":.1}
        font_size: 25

Solution

Solution

  1. Define a class property MY_GLOBAL in your root widget class ScreenManagement() of type StringProperty.
  2. Update the MY_GLOBAL in the kv file.

Example

Master .py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty

class ChooseProgScreen(Screen):
    pass

class SwitchScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    MY_GLOBAL = StringProperty('test')

class MainApp(App):

def build(self):
        return Builder.load_file("kivy.kv")

if __name__ == "__main__":
    MainApp().run()

kivy.kv

#:kivy 1.10.0
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    ChooseProgScreen:
    SwitchScreen:

<ChooseProgScreen>:
    name: "chooseprog"
    FloatLayout:
        Button:
            text: "test1"
            on_release:
                root.manager.MY_GLOBAL = self.text
                root.manager.current = "switch"
            color: 1,1,1,1
            font_size: 25
            size_hint: 0.15,0.15
            pos_hint: {"center_x":.1, "center_y":.9}
        Button:
            text: "test2"
            on_release:
                root.manager.MY_GLOBAL = self.text
                root.manager.current = "switch"
            color: 1,1,1,1
            font_size: 25
            size_hint: 0.15,0.15
            pos_hint: {"center_x":.3, "center_y":.9}

<SwitchScreen>:
    name: "switch"
    FloatLayout:
        Label:
            text: root.manager.MY_GLOBAL
            pos_hint: {"center_x":.1, "center_y":.1}
            font_size: 25

Output

Img01 - test1

Related Problems and Solutions