0

所以我正在创建一个应用程序,我需要这个东西:

小部件 1: - 带有来自 JSON 文件的数据的 GridLayout,每个数据行都转到一个按钮,所以基本上当您单击按钮时会出现一个弹出框。- 弹出:这个包含一个数字键盘输入密码,然后你点击一个按钮进入主小部件

主小部件: - 这个从 JSON 文件中读取数据,然后将其放在网格布局中,就像在小部件 1 上一样

我可以很好地用python而不是kv语言来做小部件,我只是不能做一件事:从小部件1更改为主要小部件......请帮助我,我被这个东西困扰了很长时间...

4

1 回答 1

2

In order to change between screens you just need to use the current property. Basically you have to tell the ScreenManager which is the current screen but first you have to put a a name on them. Here you have an example:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Phone>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                Label: 
                    text: 'The first screen'
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Phone(FloatLayout):
    pass

class TestApp(App):
    def build(self):
        return Phone()

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

The line

on_press: _screen_manager.current = 'screen1'

will tell the screen manager to change the screen named 'screen1' with this other line

name: 'screen1'
于 2013-07-10T18:21:18.157 回答