我正在尝试创建一个包含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)
答案 0 :(得分:-1)
我的代码似乎不起作用。我可能会遗漏一些东西。
使用提供的代码运行应用程序,在左下角显示一个小窗口。
<TrafGridLayout>:
缺少cols
或rows
属性。PageLayout
中,通过从右侧或左侧的边框区域滑入来实现从一个页面到下一个页面的转换。因此,最好使用Button
小部件on_press
事件。GridLayout必须始终至少有一个输入约束: GridLayout.cols或GridLayout.rows。
PageLayout类用于创建简单的多页面 布局,允许从一个页面轻松翻转到另一个页面 使用边框。
从一个页面到下一个页面的转换是通过从中刷入来完成的 右侧或左侧的边界区域。
将cols: 1
添加到班级规则<TrafGridLayout>:
<TrafGridLayout>:
cols: 1
PageLayout:
我想使用按钮在两个页面之间切换:当a 按钮按下页面切换。
使用Kivy ScreenManager并按下按钮切换页面。
以下示例说明如何使用Kivy ScreenManager和按钮事件之一on_press
来切换页面/屏幕。
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()
#: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"