Python Kivy中的滑块小部件用法示例

Kivy是Python中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

??Kivy教程–通过示例学习Kivy。

滑杆:

要使用滑块, 首先必须导入包含滑块的所有功能和功能的模块, 即

Module: kivy.uix.slider

Slider小部件看起来像我们在android中使用的一样, 以增加亮度, 音量等。它支持水平和垂直方向, 最小/最大值和默认值。 Kivy支持多个滑块小部件选项, 用于自定义要在不同方向上使用的光标, 光标图像, 边框, 背景, 最小值和最大值之间的区域。

Kivy还支持处理归一化值(范围0到1), 而不是滑块支持的实际范围。

创建Slider时要遵循的基本方法:1)导入kivy 2)导入kivy App 3)导入gridlayout(根据需要不是强制性的)4)导入Label(根据需要不是强制性的)5)导入Slider 6)导入数字属性7)设置最低版本(可选)8)扩展类9)添加并返回小部件10)运行该类的实例

以下是代码实现滑块:

# import kivy module 
import kivy 
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require( "1.9.1" )
  
# Kivy Example App for the slider widget
from kivy.app import App
  
# The GridLayout arranges children in a matrix.
from kivy.uix.gridlayout import GridLayout
  
# If we will not import this module
# It will through the error
from kivy.uix.slider import Slider
  
# The Label widget is for rendering text.
from kivy.uix.label import Label
  
# Property that represents a numeric value
# within a minimum bound and /or maximum
# bound – within a numeric range.
from kivy.properties  import NumericProperty
  
# class in which we are defining the
# sliders and its effects
class WidgetContainer(GridLayout):
  
     def __init__( self , * * kwargs):
          
         # super function can be used to gain access 
         # to inherited methods from a parent or sibling 
         # class that has been overwritten in a class object. 
         super (WidgetContainer, self ).__init__( * * kwargs)
  
         # 4 columns in grid layout
         self .cols = 4
          
         # declaring the slider and adding some effects to it
         self .brightnessControl = Slider( min = 0 , max = 100 )
           
  
         # 1st row - one label, one slider    
         self .add_widget(Label(text = 'brightness' ))
         self .add_widget( self .brightnessControl) 
  
         # 2nd row - one label for caption, # one label for slider value
         self .add_widget(Label(text = 'Slider Value' ))
         self .brightnessValue = Label(text = '0' )
         self .add_widget( self .brightnessValue)
  
  
         # On the slider object Attach a callback
         # for the attribute named value
         self .brightnessControl.bind(value = self .on_value)
         
     # Adding functionality behind the slider
     # i.e when pressed increase the value
     def on_value( self , instance, brightness):
         self .brightnessValue.text = "% d" % brightness
  
  
# The app class
class SliderExample(App):
     def build( self ):
         widgetContainer = WidgetContainer()
         return widgetContainer
   
  
# creating the object root for ButtonApp() class  
root = SliderExample()
    
# run function runs the whole program 
# i.e run() method which calls the 
# target function passed to the constructor. 
root.run()

输出如下:

Python Kivy中的滑块小部件1

要为滑块添加一些样式和颜色, 只需将42号行替换为下面的行, 并根据需要添加一些新功能。对于文本, 样式在文本部分使用适当的命令。

# declaring the slider and adding some effects to it
# By default its orientation is horizontal 
# if want to change to vertical do like below
         self .brightnessControl = Slider( min = 0 , max = 100 , orientation = 'vertical' , value_track = True , value_track_color = [ 1 , 0 , 0 , 1 ])

输出如下:

Python Kivy中的滑块小部件2

解释滑块如何工作的视频–

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


来源:

https://www.srcmini02.com/69944.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?