个性化阅读
专注于IT技术分析

Python Kivy中的微调小部件用法示例

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 Label
4) import Spinner
5) import Floatlayout
6) Set minimum version(optional)
7) create App class:
        1) Create the spinner
        2) Attach the labels to spinners
        3) Attach a callback also 
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class

一个简单的微调器的实现:

# Sample spinner app in kivy 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' )
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# 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 consist the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
  
# Make an App by deriving from the App class
class SpinnerExample(App):
  
     # define build 
     def build( self ):
  
         # creating floatlayout
         layout = FloatLayout()
  
         # creating the spinner
         # configure spinner object and add to layout
         self .spinnerObject = Spinner(text = "Python" , values = ( "Python" , "Java" , "C++" , "C" , "C#" , "PHP" ), background_color = ( 0.784 , 0.443 , 0.216 , 1 )) 
  
         self .spinnerObject.size_hint = ( 0.3 , 0.2 )
  
         self .spinnerObject.pos_hint = { 'x' : . 35 , 'y' :. 75 }
  
         layout.add_widget( self .spinnerObject)
  
         # return the layout
         return layout;
  
  
# Run the app
if __name__ = = '__main__' :
     SpinnerExample().run()

输出如下:

图片1

Python Kivy中的微调小部件1

图片2:

Python Kivy中的微调小部件2

现在, 如果每次都要告诉用户选择列表中的哪个元素, 我们将在微调器旁边显示一个标签, 告诉你所选择的标签。另外, 我们将打印值和微调框的文本。

下面是实现:

# Sample spinner app in kivy 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' )
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# 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 consist the floatlayout 
# to work with FloatLayout first 
# you have to import it 
from kivy.uix.floatlayout import FloatLayout
  
  
# Make an App by deriving from the App class
class SpinnerExample(App):
  
     # define build 
     def build( self ):
  
         # creating floatlayout
         layout = FloatLayout()
  
         # creating the spinner
         # configure spinner object and add to layout
         self .spinnerObject = Spinner(text = "Python" , values = ( "Python" , "Java" , "C++" , "C" , "C#" , "PHP" ), background_color = ( 0.784 , 0.443 , 0.216 , 1 )) 
  
         self .spinnerObject.size_hint = ( 0.3 , 0.2 )
  
         self .spinnerObject.pos_hint = { 'x' : . 35 , 'y' :. 75 }
  
         layout.add_widget( self .spinnerObject)
         self .spinnerObject.bind(text = self .on_spinner_select)
  
         # It changes the label info as well
         # add a label displaying the selection from the spinner
         self .spinnerSelection = Label(text = "Selected value in spinner is: %s" 
                                                      % self .spinnerObject.text)
  
         layout.add_widget( self .spinnerSelection)
         self .spinnerSelection.pos_hint = { 'x' : . 1 , 'y' :. 3 }
          
         return layout;
  
     # call back for the selection in spinner object
     def on_spinner_select( self , spinner, text):
         self .spinnerSelection.text = "Selected value in spinner is: %s"
                                               % self .spinnerObject.text)
  
         print ( 'The spinner' , spinner, 'have text' , text)
      
  
# Run the app
if __name__ = = '__main__' :
     SpinnerExample().run()

输出如下:

图片1

Python Kivy中的微调小部件3

图片2:

Python Kivy中的微调小部件4

以下是视频中的输出, 可以帮助你更好地理解:

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(1) 打赏
未经允许不得转载:srcmini » Python Kivy中的微调小部件用法示例
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏