📌  相关文章
📜  Python|使用 .kv 文件的滑块小部件

📅  最后修改于: 2022-05-13 01:55:35.399000             🧑  作者: Mango

Python|使用 .kv 文件的滑块小部件

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。

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

Module: kivy.uix.slider

创建滑块时要遵循的基本方法 -

1) import kivy
2) import kivyApp
3) import BoxLayout
4) set minimum version(optional)
5) Extend the class
6) set up .kv file (name same as the Slider.kv)
7) Run an instance of the class

下面是使用 .kv 文件实现滑块的代码:

# main.py file of slider 
  
# base Class of your App inherits from the App class.  
# app:always refers to the instance of your application  
from kivy.app import App
  
# BoxLayout arranges children in a vertical or horizontal box. 
# or help to put the children at the desired location. 
from kivy.uix.boxlayout import BoxLayout 
  
  
# creating the root widget used in .kv file 
class SliderWidget(BoxLayout):
    pass
  
# class in which name .kv file must be named Slider.kv.
# or creating the App class
class Slider(App):
    def build(self):
        # returning the instance of SliderWidget class 
        return SliderWidget()
  
# run the app    
if __name__ == '__main__':
    Slider().run()


现在是 .kv 文件: Slider.kv文件

:
  
    # creating the Slider
    Slider:
          
        # giving the orientation of Slider
        orientation: "vertical"
        min: 0  # minimum value of Slider
        max: 100 # maximum value of Slider
        value: 0  # initial value of Slider
  
        # when slider moves then to increase value
        on_value:label1.text = str(int(self.value))
  
    # Adding label
    Label:
        id: label1
        font_size: "30sp"
        text: "0"
        color: 1, 0, 0, 1
  
    Slider:
        orientation: "horizontal"
        min: 0
        max: 100
        value: 30
        on_value:label2.text = str(int(self.value))
          
  
    Label:
        id: label2
        font_size: "30sp"
        text: "30"
        color: 0, 0, 1, 1

输出:

对于没有 .kv 文件的滑块,请参阅 - Python | Kivy 中的滑块小部件