2023年11月3日 星期五

Kivy screen篇 CardTransition 講解

簡述

根據官方解釋:

Card transition that looks similar to Android 4.x application drawer interface animation.

意思是CardTransition是一個看起來像Android 4.x系統的切換方式。

基本範例

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

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


class AScreen(Screen):
    pass


class BScreen(Screen):
    pass


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


class MyApp(App):

    def build(self):
        return sm()


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

在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:

執行結果如下:

CardTransition使用技巧:

1.mode:決定如何切換Screen。

mode='push'(預設值,為基本範例所示)代表推入即將進入的Screen

mode='pop'代表將即將離開的Screen拉出畫面

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

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


class AScreen(Screen):
    pass


class BScreen(Screen):
    pass


class sm(ScreenManager):
    a1 = ObjectProperty(CardTransition(duration=3, mode='pop'))


class MyApp(App):

    def build(self):
        return sm()


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

執行結果如下:


沒有留言:

張貼留言

精選文章

Kivy UIX篇 widget篇 TabbedPanel類 event篇 講解