📌  相关文章
📜  Python| Kivy 中的 ScreenManager 使用 .kv 文件

📅  最后修改于: 2022-05-13 01:54:56.350000             🧑  作者: Mango

Python| Kivy 中的 ScreenManager 使用 .kv 文件

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

ScreenManager 小部件:

屏幕管理器是一个小部件,用于管理应用程序的多个屏幕。默认的 ScreenManager 一次只显示一个 Screen,并使用 TransitionBase 从一个 Screen 切换到另一个 Screen。支持多个转换。
ScreenManager 和 Screen 类被导入。 ScreenManager 将用于根,如:

from kivy.uix.screenmanager import ScreenManager, Screen

注意:默认 ScreenManager.transition 是具有选项方向和持续时间的 SlideTransition。

Basic Approach:
1) import kivy
2) import kivyApp
3) import Screen Manager, Screen, ""Transitions you want to use"" 
4) Set minimum version(optional)
5) Create Different Screen classes and pass them
6) Create features of Screen classes in .kv file 
       :: Add features in different screens
7) Create App class
8) return screen manager
9) Run an instance of the class

下面是 .py 文件中 .kv 文件的代码实现。

Python3
# Program to Show how to create a switch
# import kivy module   
import kivy 
      
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# Builder is used when .kv file is
# to be used in .py file
from kivy.lang import Builder
 
# The screen manager is a widget
# dedicated to managing multiple screens for your application.
from kivy.uix.screenmanager import ScreenManager, Screen
  
# You can create your kv code in the Python file
Builder.load_string("""
:
    BoxLayout:
        Button:
            text: "Go to Screen 2"
            background_color : 0, 0, 1, 1
            on_press:
                # You can define the duration of the change
                # and the direction of the slide
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_two'
  
:
    BoxLayout:
        Button:
            text: "Go to Screen 3"
            background_color : 1, 1, 0, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_three'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 4"
            background_color : 1, 0, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_four'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 5"
            background_color : 0, 1, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_five'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 1"
            background_color : 1, 0, 0, 1
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'screen_one'
 
 
""")
  
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
    pass
  
class ScreenTwo(Screen):
    pass
 
class ScreenThree(Screen):
    pass
 
class ScreenFour(Screen):
    pass
 
class ScreenFive(Screen):
    pass
  
  
# The ScreenManager controls moving between screens
screen_manager = ScreenManager()
  
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name ="screen_one"))
screen_manager.add_widget(ScreenTwo(name ="screen_two"))
screen_manager.add_widget(ScreenThree(name ="screen_three"))
screen_manager.add_widget(ScreenFour(name ="screen_four"))
screen_manager.add_widget(ScreenFive(name ="screen_five"))
 
# Create the App class
class ScreenApp(App):
    def build(self):
        return screen_manager
 
# run the app
sample_app = ScreenApp()
sample_app.run()


Python3
# Program to Show how to create a switch
# import kivy module   
import kivy 
      
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# Builder is used when .kv file is
# to be used in .py file
from kivy.lang import Builder
 
# The screen manager is a widget
# dedicated to managing multiple screens for your application.
from kivy.uix.screenmanager import (ScreenManager, Screen, NoTransition,
SlideTransition, CardTransition, SwapTransition,
FadeTransition, WipeTransition, FallOutTransition, RiseInTransition)
  
# You can create your kv code in the Python file
Builder.load_string("""
:
    BoxLayout:
        Button:
            text: "Go to Screen 2"
            background_color : 0, 0, 1, 1
            on_press:
                # You can define the duration of the change
                # and the direction of the slide
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_two'
  
:
    BoxLayout:
        Button:
            text: "Go to Screen 3"
            background_color : 1, 1, 0, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_three'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 4"
            background_color : 1, 0, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_four'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 5"
            background_color : 0, 1, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_five'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 1"
            background_color : 1, 0, 0, 1
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'screen_one'
 
 
""")
  
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
    pass
  
class ScreenTwo(Screen):
    pass
 
class ScreenThree(Screen):
    pass
 
class ScreenFour(Screen):
    pass
 
class ScreenFive(Screen):
    pass
  
  
# The ScreenManager controls moving between screens
# You can change the transitions accordingly
screen_manager = ScreenManager(transition = RiseInTransition())
  
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name ="screen_one"))
screen_manager.add_widget(ScreenTwo(name ="screen_two"))
screen_manager.add_widget(ScreenThree(name ="screen_three"))
screen_manager.add_widget(ScreenFour(name ="screen_four"))
screen_manager.add_widget(ScreenFive(name ="screen_five"))
 
# Create the App class
class ScreenApp(App):
    def build(self):
        return screen_manager
 
# run the app
sample_app = ScreenApp()
sample_app.run()


输出:

改变过渡:

您可以通过更改 ScreenManager.transition 属性轻松切换过渡:

sm = ScreenManager(transition=FadeTransition())

Python3

# Program to Show how to create a switch
# import kivy module   
import kivy 
      
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# Builder is used when .kv file is
# to be used in .py file
from kivy.lang import Builder
 
# The screen manager is a widget
# dedicated to managing multiple screens for your application.
from kivy.uix.screenmanager import (ScreenManager, Screen, NoTransition,
SlideTransition, CardTransition, SwapTransition,
FadeTransition, WipeTransition, FallOutTransition, RiseInTransition)
  
# You can create your kv code in the Python file
Builder.load_string("""
:
    BoxLayout:
        Button:
            text: "Go to Screen 2"
            background_color : 0, 0, 1, 1
            on_press:
                # You can define the duration of the change
                # and the direction of the slide
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_two'
  
:
    BoxLayout:
        Button:
            text: "Go to Screen 3"
            background_color : 1, 1, 0, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_three'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 4"
            background_color : 1, 0, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_four'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 5"
            background_color : 0, 1, 1, 1
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_five'
 
:
    BoxLayout:
        Button:
            text: "Go to Screen 1"
            background_color : 1, 0, 0, 1
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'screen_one'
 
 
""")
  
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
    pass
  
class ScreenTwo(Screen):
    pass
 
class ScreenThree(Screen):
    pass
 
class ScreenFour(Screen):
    pass
 
class ScreenFive(Screen):
    pass
  
  
# The ScreenManager controls moving between screens
# You can change the transitions accordingly
screen_manager = ScreenManager(transition = RiseInTransition())
  
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name ="screen_one"))
screen_manager.add_widget(ScreenTwo(name ="screen_two"))
screen_manager.add_widget(ScreenThree(name ="screen_three"))
screen_manager.add_widget(ScreenFour(name ="screen_four"))
screen_manager.add_widget(ScreenFive(name ="screen_five"))
 
# Create the App class
class ScreenApp(App):
    def build(self):
        return screen_manager
 
# run the app
sample_app = ScreenApp()
sample_app.run()

注意:代码是相同的,在代码中添加了一些点不要混淆。
不同转场的输出视频 –