我正在尝试创建一个有两个页面的应用程序。我想用一个按钮在两个页面之间切换:当一个按钮被按下时,页面切换。我的代码似乎不起作用。我可能遗漏了什么。 我怎样才能做到这一点?在
这是我正在使用的代码:
<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
问题
使用提供的代码运行应用程序,在左下角显示一个小窗口。在
根本原因
<TrafGridLayout>:
缺少cols
或rows
属性。在PageLayout
中,从一个页面到下一个页面的转换是通过从右侧或左侧的边界区域滑入来实现的。因此,使用Button
小部件的on_press
事件不是最佳实践。在Kivy GridLayout » constraint cols or rows
Kivy » PageLayout
解决方案-kv
将
cols: 1
添加到类规则,<TrafGridLayout>:
片段
输出
问题2
解决方案
使用KivyScreenManager并按下按钮来切换页面。在
示例
下面的示例演示了Kivy ScreenManager和button的一个事件
on_press
来切换页面/屏幕。在在主.py在
^{pr2}$在试验电压在
输出
相关问题 更多 >
编程相关推荐