Python kivy两个GridLayouts和Screen Menager

时间:2017-06-28 22:41:03

标签: python python-2.7 kivy

我在kivy有两个应用程序。 两者都基于GridLayout 我发现了类似的问题,但我不明白:Associating Screens with GridLayout classes in kivy

.py

import kivy
kivy.require("1.9.0")
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.uix.gridlayout import GridLayout

class FirstScreen(GridLayout):
    #some methods
class SecondScreen(GridLayout):
    #some methods

.kv

<FirstScreen>:
    id: sterowanie_serv
    display: entry
    rows: 10
    padding: 10
    spacing: 10
    BoxLayout:
        spacing: 20
        CustButton:
            text:'1'
            on_press: do method from second screen class
        CustButton:
            text:'x'
            on_press: do method from second screen class
        CustButton:
            text:'go to Second screen'
            on_press: root.manager.current = 'SecondScreen'

<SecondScreen>:
    id: Przemo
    display: entry
    rows: 5
    padding: 10
    spacing: 10
    BoxLayout:
        spacing: 20
        CustButton:
            text:'1'
            on_press: do method from second screen class
        CustButton:
            text:'x2'
            on_press: do method from second screen class
        CustButton:
            text:'go to First screen'
            on_press: root.manager.current = 'SecondScreen'

如何使用屏幕管理器首先显示第一个屏幕并单击按钮第二个屏幕

接下来会是:

on_press: 
    root.manager.current = 'SecondScreen'

1 个答案:

答案 0 :(得分:1)

ScreenManager仅接受Screen个小部件。 FirstScreenSecondScreen必须是Screen个实例,它们不能是GridLayouts。在FirstScreenSecondScreen内,您应该创建GridLayouts。

另一方面,ScreenManager.current属性是当前显示的屏幕的名称。您需要在Windows中设置名称属性。

基于您的代码的示例:

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

kv_text = """\
#:import WipeTransition kivy.uix.screenmanager.WipeTransition
<MainScreen>:
    transition: WipeTransition()
    id: sm
    FirstScreen:
    SecondScreen:

<FirstScreen>:
    name: "first_screen"
    GridLayout:
        id: sterowanie_serv
        rows: 10
        padding: 10
        spacing: 10
        BoxLayout:
            spacing: 20
            Button:
                text:'1'
                on_press: print(app.root)
            Button:
                text:'x'
                on_press: print('Button X')
            Button:
                text:'go to Second screen'
                on_press: app.root.current = 'second_screen'

<SecondScreen>:
    name: "second_screen"
    GridLayout:
        id: Przemo
        rows: 5
        padding: 10
        spacing: 10
        BoxLayout:
            spacing: 20
            Button:
                text:'1'
                on_press: print('Button 1')
            Button:
                text:'x2'
                on_press: print('Button X2')
            Button:
                text:'go to First screen'
                on_press: app.root.current = 'first_screen'
"""

class MainScreen(ScreenManager):
    def __init__(self):
        super(MainScreen, self).__init__()

class FirstScreen(Screen):
    #some methods
    pass

class SecondScreen(Screen):
    #some methods
    pass

class MyKivyApp(App):
    def build(self):
        return MainScreen()

def main():
    Builder.load_string(kv_text)
    app = MyKivyApp()
    app.run()

if __name__ == '__main__':
    main()

运行示例:

enter image description here