2023年10月24日 星期二

Kivy screen篇 SlideTransition 講解

簡述

根據官方解釋:

Slide Transition, can be used to show a new screen from any direction:left, right, up or down.

意思是SlideTransition可用於從任何方向顯示新屏幕。

基本範例

(大部分與ScreenManager篇中範例相同,只差在a1 = ObjectProperty()中指定的Transition不同)

在main.py中寫上此段程式碼:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.properties import ObjectProperty


class AScreen(Screen):
    pass


class BScreen(Screen):
    pass


class sm(ScreenManager):
    a1 = ObjectProperty(SlideTransition(duration=3))


class MyApp(App):

    def build(self):
        return sm()


if __name__ == '__main__':
    MyApp().run()

在以上程式碼中,我在sm類中指定a1為ObjectProperty類,其值為SlideTransition(duration=3),以便讓my.kv中sm的參數transition設定為此值

在my.kv中寫上此段程式碼:

<AScreen>:
    name: 'menu'

    BoxLayout:

        Button:
            text: 'Goto settings'
            on_press: root.manager.current = 'settings'

        Label:
            text: str(root.transition_progress)


<BScreen>:
    name: 'settings'

    BoxLayout:

        Label:
            text: str(root.transition_progress)

        Button:
            text: 'still settings'
            on_press: root.manager.current = 'menu'


<sm>:
    transition: root.a1

    AScreen:

    BScreen:

執行結果如下:

SlideTransition使用技巧:

1.direction:決定下一個Screen往哪個方向出來,可以填入的值為left, right, up or down擇一

在main.py中寫上此段程式碼:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.properties import ObjectProperty


class AScreen(Screen):
    pass


class BScreen(Screen):
    pass


class sm(ScreenManager):
    a1 = ObjectProperty(SlideTransition(duration=3, direction='up'))


class MyApp(App):

    def build(self):
        return sm()


if __name__ == '__main__':
    MyApp().run()

執行結果如下:


沒有留言:

張貼留言

精選文章

Kivy UIX篇 widget篇 TabbedPanel類 event篇 講解