【问题标题】:Python kivy two GridLayouts and Screen MenagerPython kivy 两个 GridLayout 和 Screen Manager
【发布时间】:2017-06-28 22:41:02
【问题描述】:

我在 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'

如何使用屏幕管理器先显示First Screen并点击按钮Second Screen显示?

在按钮将是:

on_press: 
    root.manager.current = 'SecondScreen'

【问题讨论】:

    标签: python python-2.7 kivy


    【解决方案1】:

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

    另一方面,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()
    

    运行示例:

    【讨论】:

    • 在按钮/滑块中我如何引用方法?我在 control_serv.py 文件中有所有类。在 kv 中,我有滑块值:control_serv.get_val_3 () 但我看到 AttributeError: 'GridLayout' object has no attribute 'get_val_3'
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多