Kivy中的SlideTransition - 滑动方向失败

时间:2017-10-26 11:28:23

标签: python-3.x kivy kivy-language

我在Kivy中使用SlideTransition遇到滑动方向问题。在实现按钮特定的滑动方向时,方向似乎只是偶尔正常工作。但是,它在其他情况下失败了。

要重现此问题,请随时使用以下代码:

main.py:

from kivy.app import App # Use of fields and methods of Kivy
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class Screen3(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("style.kv")

class MyApp(App):
    def build(self):
        return presentation

myApp = MyApp()
myApp.run()

style.kv:

#: import SlideTransition kivy.uix.screenmanager.SlideTransition

ScreenManagement:
    transition: SlideTransition()
    Screen1:
    Screen2:
    Screen3:

<Screen1>:
    name: "screen1"
    FloatLayout:
        Label:
            text: "Screen 1"
            pos_hint: {"top": 1.0}
        Button:
            on_release: 
                app.root.current = "screen2"
                app.root.transition = SlideTransition(direction="left")
            size_hint: 0.5, 0.8/3
            pos_hint: {"bottom": 0.8, "left": 1}
            text: "Screen 2 (slide left)"
            font_size: 30

<Screen2>:
    name: "screen2"

    FloatLayout:
        Label:
            text: "Screen 2"
            pos_hint: {"top": 1.0}

        Button:
            on_release: 
                app.root.current = "screen1"
                app.root.transition = SlideTransition(direction="right")
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "right": 1}
            text: "Screen 1 (slide right)"
            font_size: 30

        Button:
            on_release:
                app.root.current = "screen3"
                app.root.transition = SlideTransition(direction="left")
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "left": 1}
            text: "Screen 3 (slide left)"
            font_size: 30

<Screen3>:
    name: "screen3"

    FloatLayout:
        Label:
            text: "Screen 3"
            pos_hint: {"top": 1.0}
        Button:
            on_release: 
                app.root.current = "screen1"
                app.root.transition = SlideTransition(direction="right")
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "left": 1}
            text: "Screen 1 (slide right)"
            font_size: 30

我遇到以下问题:点击&#34;幻灯片2&#34;在第一张幻灯片和&#34;幻灯片3&#34;在第二张幻灯片中,按照指定消失在左侧,单击&#34;幻灯片1&#34;在第三张幻灯片中导致滑动方向&#34;左&#34;,尽管指定的方向是&#34;右&#34;。有什么似乎超越了滑动方向?

有没有人遇到过类似的问题而且知道如何解决这些问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题

屏幕显示在给定方向之前。因此,它使用先前的幻灯片切换方向,即左侧。你在几个地方遇到同样的问题。

Button:
    on_release: 
        app.root.current = "screen1"
        app.root.transition = SlideTransition(direction="right")

解决方案

首先设置“幻灯片切换”方向,然后将当前设置为所需的屏幕名称。有关详细信息,请参阅示例。

Button:
    on_release:
        app.root.transition = SlideTransition(direction="right")
        app.root.current = "screen1"

实施例

style.kv

#:import SlideTransition kivy.uix.screenmanager.SlideTransition

ScreenManagement:
    transition: SlideTransition()
    Screen1:
    Screen2:
    Screen3:

<Screen1>:
    name: "screen1"
    FloatLayout:
        Label:
            text: "Screen 1"
            pos_hint: {"top": 1.0}
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen2"
            size_hint: 0.5, 0.8/3
            pos_hint: {"bottom": 0.8, "left": 1}
            text: "Screen 2 (slide left)"
            font_size: 30

<Screen2>:
    name: "screen2"

    FloatLayout:
        Label:
            text: "Screen 2"
            pos_hint: {"top": 1.0}

        Button:
            on_release:
                app.root.transition = SlideTransition(direction="right")
                app.root.current = "screen1"
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "right": 1}
            text: "Screen 1 (slide right)"
            font_size: 30

        Button:
            on_release:
                app.root.transition = SlideTransition(direction="left")
                app.root.current = "screen3"
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "left": 1}
            text: "Screen 3 (slide left)"
            font_size: 30

<Screen3>:
    name: "screen3"

    FloatLayout:
        Label:
            text: "Screen 3"
            pos_hint: {"top": 1.0}
        Button:
            on_release:
                app.root.transition = SlideTransition(direction="right")
                app.root.current = "screen1"
            size_hint: 0.5, 0.8/3
            pos_hint: {"top": 0.8, "left": 1}
            text: "Screen 1 (slide right)"
            font_size: 30