kivy 按钮按下以转到多个屏幕之一
Posted
技术标签:
【中文标题】kivy 按钮按下以转到多个屏幕之一【英文标题】:kivy button press to go to one of multiple screens 【发布时间】:2018-01-25 09:27:35 【问题描述】:假设我有一个 kivy 程序,其中第一个屏幕有三个按钮,每个按钮都将您带到不同的屏幕。这些屏幕中的每一个都包含一个按钮,可将您带到最终屏幕(同一屏幕)。最后一个屏幕有一个“返回”按钮。如何使最后一个屏幕上的“后退”按钮带您回到前一个屏幕(上面提到的 3 个屏幕中的第一个)?下面是一个粗略的图表,可以更好地帮助可视化我的问题。
Screen 1
Start Screen --> Screen 2 --> Final Screen
Screen 3
截至目前,最终屏幕上的“返回”按钮会将您带回到“开始”屏幕,但这不是我想要的。
我的python代码:
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
class MyScreenManager(ScreenManager):
pass
# Branch Screen, takes you to S1, S2, or S3
class BRANCH(Screen):
pass
class S1(Screen):
pass
class S2(Screen):
pass
class S3(Screen):
pass
class FINAL_SCREEN(Screen):
pass
class MAINApp(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
MAINApp().run()
我的kivy语言代码:
#:kivy 1.0.9
<MyScreenManager>:
BRANCH:
name: 'branch'
S1:
name: 'screen1'
S2:
name: 'screen2'
S3:
name: 'screen3'
FINAL_SCREEN:
name: 'final_screen'
<BRANCH>
FloatLayout:
Button:
text: 'Screen-1'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.75
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen1'
Button:
text: 'Screen-2'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.40
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen2'
Button:
text: 'Screen-3'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.05
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen3'
<S1>
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'this is SCREEN-1'
color: 1, 1, 1, 1
Button:
text: 'Forward'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
<S2>
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'me este SCREEN-2'
color: 1, 1, 1, 1
Button:
text: 'Onward'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
<S3>
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'something SCREEN-3'
color: 1, 1, 1, 1
Button:
text: 'Lets Go'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
<FINAL_SCREEN>
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'Final Screen'
color: 1, 1, 1, 1
Button:
text: 'Back'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'branch'
非常感谢任何帮助或建议。
【问题讨论】:
你试过调用屏幕管理器previous()
方法吗?
不知道previous() 方法。哇。
我不知道为什么,但是 previous() 让我回到两个屏幕。
是的@PalimPalim 指出了这一点,我现在无法对其进行测试,但我认为它使用给出的屏幕列表来实例化管理器,抱歉提示错误
【参考方案1】:
这是在Final
屏幕上使用 StringProperty 的一种解决方案。
我是在进入最终画面时设置的
root.manager.ids.final.previous = root.name
当返回时,我使用它转到此屏幕
root.manager.current = root.previous
这是你的整个应用程序
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivy.properties import StringProperty
kv_str = """
MyScreenManager:
BRANCH:
name: 'branch'
S1:
name: 'screen1'
S2:
name: 'screen2'
S3:
name: 'screen3'
FINAL_SCREEN:
id: final
name: 'final_screen'
<BRANCH>
FloatLayout:
Button:
text: 'Screen-1'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.75
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen1'
Button:
text: 'Screen-2'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.40
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen2'
Button:
text: 'Screen-3'
color: 1.0, 0.6, 0.0, 1
font_size: 30
size_hint_x: 0.50
size_hint_y: 0.25
pos_hint: 'x': 0.20, 'y': 0.05
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'screen3'
<S1>:
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'this is SCREEN-1'
color: 1, 1, 1, 1
Button:
text: 'Forward'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
root.manager.ids.final.previous = root.name
<S2>:
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'me este SCREEN-2'
color: 1, 1, 1, 1
Button:
text: 'Onward'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
root.manager.ids.final.previous = root.name
<S3>:
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'something SCREEN-3'
color: 1, 1, 1, 1
Button:
text: 'Lets Go'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'final_screen'
root.manager.ids.final.previous = root.name
<FINAL_SCREEN>:
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'Final Screen'
color: 1, 1, 1, 1
Button:
text: 'Back'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
root.manager.transition.direction = 'right'
root.manager.current = root.previous
"""
class MyScreenManager(ScreenManager):
pass
# Branch Screen, takes you to S1, S2, or S3
class BRANCH(Screen):
pass
class S1(Screen):
pass
class S2(Screen):
pass
class S3(Screen):
pass
class FINAL_SCREEN(Screen):
previous = StringProperty()
class MAINApp(App):
def build(self):
return Builder.load_string(kv_str)
if __name__ == '__main__':
MAINApp().run()
我按照 cmets 中的建议尝试了 previous()
<FINAL_SCREEN>:
FloatLayout:
Label:
pos_hint: 'x': 0.00, 'y': 0.20
font_size: 35
text: 'Final Screen'
color: 1, 1, 1, 1
Button:
text: 'Back'
color: 1.0, 0.6, 0.0, 1
font_size: 20
size_hint_x: 0.20
size_hint_y: 0.20
pos_hint: 'x': 0.40, 'y': 0.15
on_release:
print(root.manager.previous())
root.manager.current = root.manager.previous()
奇怪的是,它总是返回 screen3,因此我使用了上面的版本。
【讨论】:
以上是关于kivy 按钮按下以转到多个屏幕之一的主要内容,如果未能解决你的问题,请参考以下文章
模拟 iOS 屏幕键盘上的“Go”、“Next”按钮按下以进行 xcode 测试
Kivy - 如何在 ModalView 内向 ScrollView 添加多个按钮?