设置全局变量Kivy

时间:2018-07-02 18:13:35

标签: python oop global-variables kivy

我正在使用屏幕管理器来管理几个不同的屏幕。一个屏幕上有两个按钮,都通向另一个屏幕,但是我希望标签能在第二个屏幕上显示不同的文本,具体取决于所按下的按钮。这是我的代码:

.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

在.kv的ChooseProgScreen中,释放按钮时,我从.py文件调用一个方法,该方法将全局变量设置为新屏幕并进行打印。当您按下其中一个按钮时,打印部分可以正常工作,并且全局变量将作为新字符串打印,但是SwitchScreen标签仍显示

"test"

而不是

"test1" or "test2"

我认为全局变量可能是执行此操作的一种糟糕方法,但是我对如何使用屏幕管理器和kivy语言进行操作感到困惑。如果有人可以帮助正确使用全局变量,或提出更好的方法来做到这一点,将不胜感激。

编辑

问题是屏幕仅在第一次加载时更新。我向SwitchScreen添加了更新方法:

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

并更新.kv文件中的SwitchScreen:

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

2 个答案:

答案 0 :(得分:1)

解决方案

  1. 在您的根窗口小部件ScreenManagement()中定义一个类型为StringProperty的MY_GLOBAL类属性。
  2. 更新kv文件中的MY_GLOBAL。

示例

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

输出

Img01 - test1

答案 1 :(得分:0)

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

那应该可以解决您的问题。从技术上讲,MY_GLOBAL可以通过self.MY_GLOBAL来使用,相反,您在方法下将其分配给了一个新变量,但没有将其分配给全局变量对象。