Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。
ScreenManager小部件
屏幕管理器是一个小部件, 用于管理你的应用程序的多个屏幕。默认的ScreenManager一次仅显示一个屏幕, 并使用TransitionBase从一个屏幕切换到另一个屏幕。支持多个转换。
将导入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
下面是代码的实现.kv归档在.py文件。
# 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( """
<ScreenOne>:
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'
<ScreenTwo>:
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'
<ScreenThree>:
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'
<ScreenFour>:
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'
<ScreenFive>:
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()
输出如下:
更改过渡:
默认情况下, 你有多个可用的过渡, 例如:NoTransition –无需动画即可立即切换屏幕SlideTransition –从任意方向向内/向外滑动屏幕CardTransition –新屏幕在前一个屏幕上滑动或旧屏幕在新屏幕上滑动, 具体取决于模式SwapTransition – iOS交换过渡的实现FadeTransition –着色器以淡入/淡出屏幕WipeTransition –着色器以从右向左擦除屏幕FallOutTransition –着色器, 旧屏幕”掉落”并变得透明, 向后展示新屏幕它。 RiseInTransition –着色器, 新屏幕从屏幕中心升起, 同时从透明渐变为不透明。
你可以通过更改ScreenManager.transition属性轻松切换切换:
sm = ScreenManager(transition=FadeTransition())
# 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( """
<ScreenOne>:
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'
<ScreenTwo>:
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'
<ScreenThree>:
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'
<ScreenFour>:
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'
<ScreenFive>:
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 accorsingly
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()
注意:代码相同在代码中添加了一些要点, 不要混淆。
输出不同过渡的视频–
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册