如何在第二个kivy屏幕上更新标签文本

时间:2015-11-03 11:36:01

标签: kivy

我有一个带有两个屏幕的小型kivy应用程序。我正在尝试更新第一个

的第二个标签
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty

# Declare both screens
class MainScreen(Screen):
    def SetText(self):
      text='Total='+str(17*21)
      StrategyScreen().SetText(text)

class StrategyScreen(Screen):
    labelText = StringProperty()
    labelText='My label'
    print 'start'

    def SetText(self, text):
      print 'a', self.labelText
      self.labelText=text
      print 'b', self.labelText

class TestApp(App):
    def build(self):
        # Create the screen manager
        screenManager = ScreenManager()
        screenManager.add_widget(MainScreen(name='main'))
        screenManager.add_widget(StrategyScreen(name='strategy'))
        return screenManager

if __name__ == '__main__':
    TestApp().run()

使用kv文件:

<MainScreen>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: 'Goto strategy'
            on_press: root.manager.current = 'strategy'
        Button:
            text: 'Set text'
            on_press: root.SetText()

<StrategyScreen>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: root.labelText
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'main'

从'start'判断只有一个StrategyScreen实例和SetText被正确调用,但标签仍然显示旧文本。

任何人都可以请帮助

1 个答案:

答案 0 :(得分:2)

问题在于SetText课程的MainScreen方法。调用StrategyScreen().SetText(text)会创建StrategyScreen()类型的新临时对象,在其上调用方法SetText,然后丢弃它,因为不再需要它。要使用self.manager.get_screen()方法获取现有屏幕:

class MainScreen(Screen):
    def SetText(self):
        text = 'Total=' + str(17*21)
        self.manager.get_screen('strategy').SetText(text)

您可以直接更改其属性,而不是调用StrategyScreen方法:

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

Builder.load_string('''
<MainScreen>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: 'Goto strategy'
            on_press: root.manager.current = 'strategy'
        Button:
            text: 'Set text'
            on_press: root.SetText()

<StrategyScreen>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: root.labelText
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'main'
''')

class MainScreen(Screen):
    def SetText(self):
        text = 'Total=' + str(17*21)
        self.manager.get_screen('strategy').labelText = text

class StrategyScreen(Screen):
    labelText = StringProperty('My label')

class TestApp(App):
    def build(self):
        # Create the screen manager
        screenManager = ScreenManager()
        screenManager.add_widget(MainScreen(name='main'))
        screenManager.add_widget(StrategyScreen(name='strategy'))
        return screenManager

if __name__ == '__main__':
    TestApp().run()