首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按下kivy按钮进入多个屏幕之一

按下kivy按钮进入多个屏幕之一
EN

Stack Overflow用户
提问于 2017-08-17 05:15:17
回答 1查看 1.3K关注 0票数 0

假设我有一个kivy程序,其中第一个屏幕有三个按钮,每个按钮带您到不同的屏幕。每个屏幕都包含一个按钮,将您带到最后一个屏幕(相同的屏幕)。最后一个屏幕上有一个“后退”按钮。如何使最后屏幕上的“后退”按钮将您带回到前一个屏幕(上面提到的3个屏幕中的一个)?下面是一个粗略的图表,以帮助更好地形象化我的问题。

代码语言:javascript
复制
                  Screen 1
Start Screen -->  Screen 2 --> Final Screen
                  Screen 3 

到目前为止,在最后一个屏幕上的“后退”按钮将你带回到开始屏幕,但这不是我想要的。

我的python代码:

代码语言:javascript
复制
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()

我的语言代码:

代码语言:javascript
复制
#: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'

任何帮助或建议都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-17 07:22:57

这里有一个在StringProperty屏幕上使用Final的解决方案。

当进入最后的屏幕时,我正在设置它。

代码语言:javascript
复制
root.manager.ids.final.previous = root.name

当我回去的时候,我用它转到这个屏幕上

代码语言:javascript
复制
root.manager.current = root.previous

这是你的整个应用程序

代码语言:javascript
复制
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()

我按照评论中的建议尝试了previous()

代码语言:javascript
复制
<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,因此我使用了上面的版本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45727068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档