Python kivy之二 GridLayout和Screen Manager

标签 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'

最佳答案

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

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

基于您的代码的示例:

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

关于Python kivy之二 GridLayout和Screen Manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813651/

相关文章:

python - 仅三角形内部的一些点被视为 'inside' 三角形

python - 通过 django 中的 View 将 matplotlib 图形渲染为 html

python - 当其中一行匹配条件时如何返回数据库实体的总计数

Python追加到列表问题

python - 使用转换计算数据框中的特定值和聚合结果

python - Docker中的端口映射

python - CherryPy 无法启动

python - python中错误处理的问题

python - 在 kivy 中用 Action 交换屏幕

android - 在 kivy 文件中导入小部件