Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Global variables Kivy

I am using the Screen Manager to manage several different screens. One screen has two buttons that both lead to another screen, but depending on which button was pressed, I would like a 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 that sets the global variable to a new screen and prints it. When you press one of the buttons, the print part works fine, and the global variable prints as the new string, but the SwitchScreen label still shows

"test"

and not

"test1" or "test2"

I think global variables is probably a terrible way of doing this, but I am at a loss for how else to do it using the Screen manager and the kivy language. If someone could help use global variables properly, or suggest a better way to do this, that would be greatly appreciated.

EDIT

Problem was that the screen only updated upon the first load. I added an update method to SwitchScreen:

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

and updated the SwitchScreen in the .kv file:

<SwitchScreen>:
    on_enter:
        root.update()
    Label:
        id: switchtitle
        text: root.getTitle()
        pos_hint: {"center_x":.1, "center_y":.1}
        font_size: 25
like image 515
Peter Kapteyn Avatar asked Nov 02 '25 04:11

Peter Kapteyn


1 Answers

Solution

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

Example

main.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

like image 54
ikolim Avatar answered Nov 03 '25 20:11

ikolim