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

Python多个滑块小部件在Kivy中控制背景屏幕或WindowColor

先决条件– Kivy中的滑块

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

在本文中, 我们将学习如何在kivy中控制背景颜色, 这意味着, 如果滑动滑块, 则窗口的颜色也会相应地改变。

你可以通过许多不同的方法进行此操作。我们来看一种简单的方法。

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

Basic Approach to make it is very simple:
1) import kivy
2) import kivyApp
3) import BoxLayout
4) set minimum version(optional)
5) Extend the class
6) set up .kv file :
        -> Set orientation
        -> Set slider color
        -> Create canvas.before property
        -> Create Sliders
        -> Create label
7) Return layout
8) Run an instance of the class

如何在Kivy中使用多个滑块控制背景色?

# main.py to manipulate the window 
# color or screen clour in 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
  
# BoxLayout arranges children in a vertical or horizontal box. 
# or help to put the childrens at the desired location. 
from kivy.uix.boxlayout import BoxLayout 
  
# creating the root widget used in .kv file 
class MultipleSliderWidget(BoxLayout):
     pass
  
# class in which name .kv file must be named Slider.kv.
# or creating the App class
class Multiple_Slider(App):
     def build( self ):
         # returning the instance of SliderWidget class 
         return MultipleSliderWidget()
  
# run the app    
if __name__ = = '__main__' :
     Multiple_Slider().run()

现在.kv文件上面的代码是–

# Multiple_Slider.kv file of the main.py file.
  
#.kv file to manipulate the window colour.
<MultipleSliderWidget>:
      
     # giving the orientation of Slider
     orientation: "vertical"
  
     # initially providing this colour to window
     slider_colors: 0.5 , 0.5 , 0.5
  
     # executed before the canvas group.
     canvas.before:
         Color:
             rgb: root.slider_colors
         Rectangle:
             pos: root.pos
             size: root.size
              
     # creating the Slider
     Slider:
         min : 0  # minimum value of Slider
         max : 1 # maximum value of Slider
         value: 0.5  # initial value of Slider
          
         # when slider moves then to increase value
         on_value: root.slider_colors[ 0 ] = self .value;
  
     Slider:
         min : 0
         max : 1
         value: 0.5
         on_value: root.slider_colors[ 1 ] = self .value
  
     Slider:
         min : 0
         max : 1
         value: 0.5
         on_value: root.slider_colors[ 2 ] = self .value
  
     # Adding The label 
     Label:
         font_size: "30sp"
         # the for loop is for continuously changing
         # the clour as slider value changes
         text: "Color:" + ", " .join([ "%.3f" % (i) for i in root.slider_colors]) 
         color: 0 , 0 , 1 , 1

输出如下:

Python多个滑块小部件在Kivy中控制背景屏幕或WindowColor1

视频输出:

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


赞(0)
未经允许不得转载:srcmini » Python多个滑块小部件在Kivy中控制背景屏幕或WindowColor

评论 抢沙发

评论前必须登录!