簡體   English   中英

按下kivy按鈕可轉到多個屏幕之一

[英]kivy button press to go to one of multiple screens

假設我有一個kivy程序,其中第一個屏幕具有三個按鈕,每個按鈕都將您帶到另一個屏幕。 這些屏幕中的每個屏幕都包含一個按鈕,可將您帶到最終屏幕(同一屏幕)。 最終屏幕上有一個“后退”按鈕。 如何使最后一個屏幕上的“后退”按鈕將您帶回到上一個屏幕(上述3個中的1個)? 下面是一個粗略的圖表,可以更好地幫助可視化我的問題。

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

任何幫助或建議,我們將不勝感激。

這是在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()

我按照評論中的建議嘗試了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,因此我使用了上面的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM