什么小部件更适合在kivy中使用2页应用程序?

时间:2017-11-29 18:20:21

标签: python kivy

我正在尝试创建一个包含2个页面的应用。我想使用按钮在两个页面之间切换:当按下按钮时,页面会切换。我的代码似乎不起作用。我可能会遗漏一些东西。 我怎样才能做到这一点?

这是我正在使用的代码:

<TrafGridLayout>:
    PageLayout:
        BoxLayout:
            orientation: 'vertical'

            BoxLayout:
                TextInput:
                    text: ''

            BoxLayout:
                TextInput:
                    text: ''

            # Calculate and show page #2
            BoxLayout:
                Button:
                    text: "Calculate"
                    on_press:
                        traffictax.calculate(point_from.text, point_to.text)
                        traffictax.show_page(1)

        BoxLayout:
            orientation: 'vertical'

            # Show page #1
            BoxLayout:
                Button:
                    text: "Back to first page"
                    on_press: traffictax.show_page(0)

1 个答案:

答案 0 :(得分:-1)

问题1

  

我的代码似乎不起作用。我可能会遗漏一些东西。

问题

使用提供的代码运行应用程序,在左下角显示一个小窗口。

根本原因

  1. 类规则<TrafGridLayout>:缺少colsrows属性。
  2. PageLayout中,通过从右侧或左侧的边框区域滑入来实现从一个页面到下一个页面的转换。因此,最好使用Button小部件on_press事件。
  3. Kivy GridLayout » constraint cols or rows

      

    GridLayout必须始终至少有一个输入约束:   GridLayout.colsGridLayout.rows

    Kivy » PageLayout

      

    PageLayout类用于创建简单的多页面   布局,允许从一个页面轻松翻转到另一个页面   使用边框。

         

    从一个页面到下一个页面的转换是通过从中刷入来完成的   右侧或左侧的边界区域。

    解决方案 - kv

    cols: 1添加到班级规则<TrafGridLayout>:

    片段

    <TrafGridLayout>:
        cols: 1
    
        PageLayout:
    

    输出

    PageLayout - Page 1 PageLayout - Page 2

    问题2

      

    我想使用按钮在两个页面之间切换:当a   按钮按下页面切换。

    解决方案

    使用Kivy ScreenManager并按下按钮切换页面。

    实施例

    以下示例说明如何使用Kivy ScreenManager和按钮事件之一on_press来切换页面/屏幕。

    main.py

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    
    
    class ScreenManagement(ScreenManager):
        pass
    
    
    class FirstPage(Screen):
    
        def calculate(self, point_from, point_to):
            print("point_from={0}, point_to={1}".format(point_from, point_to))
    
    
    class SecondPage(Screen):
        pass
    
    
    class TestApp(App):
        title = "Login Screen"
    
        def build(self):
            return ScreenManagement()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    
    <ScreenManagement>:
        FirstPage:
            id: page1
        SecondPage:
    
    <FirstPage>:
        name: "first_page"
    
        BoxLayout:
            orientation: 'vertical'
    
            BoxLayout:
                Label:
                    text: "Point From:"
                TextInput:
                    id: point_from
                    text: ''
    
            BoxLayout:
                Label:
                    text: "Point To:"
                TextInput:
                    id: point_to
                    text: ''
    
            # Calculate and show page #2
            BoxLayout:
                Button:
                    text: "Calculate"
                    on_press:
                        root.calculate(point_from.text, point_to.text)
                        root.manager.current = "second_page"
    
    
    <SecondPage>:
        name: "second_page"
    
        BoxLayout:
            orientation: 'vertical'
    
            # Show page #1
            BoxLayout:
                Button:
                    text: "Back to first page"
                    on_press: root.manager.current = "first_page"
    

    输出

    Figure 1 - App Startup Figure 2 - Points Entered Figure 3 - Page Two Displayed Figure 4 - Back to Page One