Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。
??Kivy教程–通过示例学习Kivy。
Switch小部件:
Switch小部件是活动的或不活动的, 作为机械灯开关。用户可以向左/向右滑动以激活/停用它。
Switch表示的值为True或False。也就是说, Switch可以处于”开”位置或”关”位置。
要使用Switch, 你必须导入:
from kivy.uix.switch import Switch
将回调附加到交换机:
- Switch可以附加有回调, 以检索Switch的值。
- 开关的状态转换是从ON到OFF或从OFF到ON。
- 当switch进行任何转换时, 将触发回调, 并且可以检索新状态(即传来新状态), 并且可以基于状态采取任何其他操作。
- 默认情况下, 小部件的表示形式是静态的。所需的最小尺寸为83 * 32像素。
- 整个窗口小部件都处于活动状态, 而不仅仅是带有图形的部分。只要你在小部件的边界框上滑动, 它就会起作用。
Basic Approach:
1) import kivy
2) import kivyApp
3) import Switch
4) import Gridlayout
5) import Label
6) Set minimum version(optional)
7) create Layout class(In this you create a switch):
--> define the callback of the switch in this
8) create App class
9) create .kv file (name same as the app class):
1) create boxLayout
2) Give Lable
3) Create Switch
4) Bind a callback if needed
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class
下面是实现:
我们已经解释了如何创建按钮, 为其添加回调以及如何在使按钮处于活动/非活动状态后禁用按钮。
main.py文件:
# Program to explain how switch works
# 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' )
# The Switch widget is active or inactive
# The state transition of a switch is from
# either on to off or off to on.
from kivy.uix.switch import Switch
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows, # then adds widgets to the resulting "cells".
from kivy.uix.gridlayout import GridLayout
# The Label widget is for rendering text.
from kivy.uix.label import Label
# A Gridlayout with a label a switch
# A class which contains all stuff about the switch
class SimpleSwitch(GridLayout):
# number of rows
rows = 4
# Callback for the switch state transition
# Defining a Callback function
# Contains Two parameter switchObject, switchValue
def switch_callback( self , switchObject, switchValue):
# Switch value are True and False
if (switchValue):
print ( 'Switch is ON:):):)' )
else :
print ( 'Switch is OFF:(:(:(' )
# Defining the App Class
class SwitchApp(App):
# define build function
def build( self ):
# retuen the switch class
return SimpleSwitch()
# Run the kivy app
if __name__ = = '__main__' :
SwitchApp().run()
.kv文件
:在此我们完成了回调并还禁用了按钮。
# .kv file in which the whole functions of a switch
# Along with labels are present
<SimpleSwitch>:
# creating box layout for better view
BoxLayout:
size_hint_y: None
height: '48dp'
# Adding label to switch
Label:
text: 'Switch normal'
# creating the switch
Switch:
# False means OFF and True means ON
active: False
# Arranging a callback to the switch
on_active: root.switch_callback( self , self .active)
# Another for another switch
BoxLayout:
size_hint_y: None
height: '48dp'
Label:
text: 'Switch active'
Switch:
active: True
on_active: root.switch_callback( self , self .active)
BoxLayout:
size_hint_y: None
height: '48dp'
Label:
text: 'Switch off & disabled'
Switch:
# disabled True means After making switch False
# it is disabled now you cannot change its state
disabled: True
active: False
BoxLayout:
size_hint_y: None
height: '48dp'
Label:
text: 'Switch on & disabled'
Switch:
disabled: True
active: True
输出如下:
图片1
图片2:
显示回调的图像:
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册