kivy中的2页应用程序使用哪个小部件更好?

2024-09-29 21:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个有两个页面的应用程序。我想用一个按钮在两个页面之间切换:当一个按钮被按下时,页面切换。我的代码似乎不起作用。我可能遗漏了什么。 我怎样才能做到这一点?在

这是我正在使用的代码:

<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)

Tags: 代码textonshowpagebutton页面textinput
1条回答
网友
1楼 · 发布于 2024-09-29 21:23:33

问题1

My code does not seem to work. I might be missing something.

问题

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

根本原因

  1. 类规则<TrafGridLayout>:缺少colsrows属性。在
  2. PageLayout中,从一个页面到下一个页面的转换是通过从右侧或左侧的边界区域滑入来实现的。因此,使用Button小部件的on_press事件不是最佳实践。在

Kivy GridLayout » constraint cols or rows

A GridLayout must always have at least one input constraint: GridLayout.cols or GridLayout.rows.

Kivy » PageLayout

The PageLayout class is used to create a simple multi-page layout, in a way that allows easy flipping from one page to another using borders.

Transitions from one page to the next are made by swiping in from the border areas on the right or left hand side.

解决方案-kv

cols: 1添加到类规则,<TrafGridLayout>:

片段

<TrafGridLayout>:
    cols: 1

    PageLayout:

输出

PageLayout - Page 1PageLayout - Page 2

问题2

I would like to switch between the two pages using a button: when a button is pressed the page switches.

解决方案

使用KivyScreenManager并按下按钮来切换页面。在

示例

下面的示例演示了Kivy ScreenManager和button的一个事件on_press来切换页面/屏幕。在

在主.py在

^{pr2}$

在试验电压在

#: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 StartupFigure 2 - Points EnteredFigure 3 - Page Two DisplayedFigure 4 - Back to Page One

相关问题 更多 >

    热门问题