繁体   English   中英

更改屏幕时如何更新屏幕值

[英]How can I update screen values when I change the screen

我有一个python脚本和一个具有不同屏幕的kivy文件。 在屏幕1中,我在TextInput引入了一些信息,当我切换到以下屏幕(屏幕2)时,我想恢复在屏幕1的TextInput中引入的变量的值。我在更新屏幕的函数__init__问题2。

我不知道哪种方法是自动更新值的最佳方法。

class PantallaDos(Screen):

    def __init__(self, *args):
        super(PantallaDos, self).__init__(**kwargs)
        softinput_mode = 'below_target'
        Window.softinput_mode = softinput_mode

        self.ids['pol'].text = 'verdadero'

Kivy kv.file

<PantallaDos>:

    BoxLayout:
        orientation: 'vertical'
        padding: 10
        spacing: 3
        canvas:
            Color:
                rgb: 0.15,0.14,0.14
            Rectangle:
                pos: self.pos
                size: self.size

....

        SLabel:
            id: pol
            size_hint: None, None
            size: root.width, 30

您可以将共享数据保存在ScreenManager类或App类中。
或者直接在kv中访问它。
这是一个示例,其中您仅使用textinput的ID(以kv为单位)在另一个屏幕上设置标签文本。

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


Builder.load_string("""

<Manager>:
    Screen:
        name: 'first'
        BoxLayout:
            TextInput:
                id: ti
                text: 'type something'
            Button:
                text:'go to other'
                on_press: app.sm.current = 'other'

    Screen:
        name: 'other'
        BoxLayout:
            Label:
                text: ti.text
            Button:
                text:'go to first'
                on_press: app.sm.current = 'first'

""")


class Manager(ScreenManager):
    pass


class ClassApp(App):

    def build(self):
        self.sm = Manager()
        return self.sm


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM