Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。
??Kivy教程–通过示例学习Kivy。
微调小部件:
要使用微调器, 你必须导入:
from kivy.uix.spinner import Spinner
Spinner是一个小部件, 提供了一种从一组中选择一个值的快速方法。在默认状态下, 微调器显示其当前选定的值。触摸微调器会显示一个下拉菜单, 其中包含所有其他可用值, 用户可以从中选择一个新值。
像组合框一样, 微调器对象可以具有多个值, 并且可以选择一个值。
回调可以附加到微调器对象上, 以接收有关从微调器对象选择值的通知。
Basic Approach:
1) import kivy
2) import kivyApp
3) import spinner
4) import Floatlayout(according to need)
5) import window(optional)
6) Set minimum version(optional)
7) Create Layout class:
define the clicked function in it
8) Create App class
9) create .kv file (name same as the app class):
1) create Spinner
2) create callback
3) And many more styling as needed
10) return Layout/widget/Class(according to requirement)
11) Run an instance of the class
下面是实现:
在下面的代码中, 我们创建了微调器, 并进行了大小调整和定位, 并将回调函数附加到值上。
.py文件:
# Sample spinner app in kivy using .kv file
# to change the kivy default settings we use this module config
from kivy.config import Config
# 0 being off 1 being on as in true /false
# you can use 0 or 1 && True or False
Config. set ( 'graphics' , 'resizable' , True )
# 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' )
# Spinner is a widget that provides a
# quick way to select one value from a set.
# like a dropdown list
from kivy.uix.spinner import Spinner
# module consists the floatlayout
# to work with FloatLayout first
# you have to import it
from kivy.uix.floatlayout import FloatLayout
# Here for providing colour to the background
from kivy.core.window import Window
# create LayoutClass
class SampBoxLayout(FloatLayout):
# For Spinner defining spinner clicked function
def spinner_clicked( self , value):
print ( "Language selected is " + value)
# # Make an App by deriving from the App class
class SampleApp(App):
def build( self ):
# Set the background color for the window
Window.clearcolor = ( 0.555 , 0.261 , . 888 , 0.5 )
return SampBoxLayout()
# create object for thje Appclass
root = SampleApp()
# run the class
root.run()
.kv文件
代码:
# .kv file implementation of the .py file
# Creating the Layout i.e root of the Layout class
<SampBoxLayout>:
# creating the spinner
Spinner:
# Assigning id
id : spinner_id
# Callback
on_text: root.spinner_clicked(spinner_id.text)
# initially text on spinner
text: "Python"
# total values on spinner
values: [ "Python" , "Java" , "C++" , "C" , "C#" , "PHP" ]
# declaring size of the spinner
# and the position of it
size_hint: None , None
size: 200 , 50
pos_hint:{ 'center_x' :. 5 , 'top' : 1 }
输出如下:
图片1
图片2:
图片3:
以下是视频中的输出, 可以帮助你更好地理解:
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册