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

Python如何在Kivy中切换小部件?

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

👉🏽Kivy教程–通过示例学习Kivy。

Switch小部件:

Switch小部件是活动的或不活动的, 作为机械灯开关。用户可以向左/向右滑动以激活/停用它。开关表示的值为True或False。那就是开关可以在On位置或关位置。

要使用Switch, 你必须导入:

from kivy.uix.switch import Switch

注意:如果要通过单次触摸而不是滑动来控制状态, 请改用ToggleButton。

Basic Approach:

1) import kivy
2) import kivyApp
3) import Switch
4) import Gridlayout
5) import Label
6) Set minimum version(optional)
7) create Layout class(In this you create a switch)
8) create App class
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class

该方法的实施:

# 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 Switch widget is active or inactive
# The state transition of a switch is from
# either on to off or off to on.
from kivy.uix.switch import Switch
  
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows, # then adds widgets to the resulting "cells".
from kivy.uix.gridlayout import GridLayout
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# A Gridlayout with a label a switch
# A class which contains all stuff about the switch 
class SimpleSwitch(GridLayout):
  
      # Defining __init__ constructor
      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 (SimpleSwitch, self ).__init__( * * kwargs)
  
           # no of coloumns 
           self .cols = 2
  
           # Adding label to the Switch
           self .add_widget(Label(text = "Switch" ))
  
           # Initially switch is Off i.e active = False
           self .settings_sample = Switch(active = False )
  
           # Add widget
           self .add_widget( self .settings_sample)
  
             
# Defining the App Class
class SwitchApp(App):
      # define build function
      def build( self ):
           # retuen the switch class
           return SimpleSwitch()
  
   
# Run the kivy app
if __name__ = = '__main__' :
      SwitchApp().run()

输出如下:

Python |在Kivy中切换小部件1
Python |在Kivy中切换小部件2

将回调附加到交换机:

  • 开关可以附加有回调, 以检索开关的值。
  • 开关的状态转换是从ON到OFF或从OFF到ON。
  • 当switch进行任何转换时, 将触发回调, 并且可以检索新状态(即传来新状态), 并且可以基于状态采取任何其他操作。
  • 默认情况下, 小部件的表示形式是静态的。所需的最小尺寸为83 * 32像素。
  • 整个窗口小部件都处于活动状态, 而不仅仅是带有图形的部分。只要你在小部件的边界框上滑动, 它就会起作用。

现在, 要附加回调, 你必须定义一个回调函数并将其与开关绑定。因此, 以下代码是如何附加回调的代码:

# Program to Show how to attach a callback to 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 Switch widget is active or inactive
# The state transition of a switch is from
# either on to off or off to on.
from kivy.uix.switch import Switch
  
# The GridLayout arranges children in a matrix.
# It takes the available space and
# divides it into columns and rows, # then adds widgets to the resulting "cells".
from kivy.uix.gridlayout import GridLayout
  
# The Label widget is for rendering text. 
from kivy.uix.label import Label
  
# A Gridlayout with a label a switch
# A class which contains all stuff about the switch 
class SimpleSwitch(GridLayout):
  
      # Defining __init__ constructor
      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 (SimpleSwitch, self ).__init__( * * kwargs)
  
           # no of coloumns 
           self .cols = 2
  
           # Adding label to the Switch
           self .add_widget(Label(text = "Switch" ))
  
           # Initially switch is Off i.e active = False
           self .settings_sample = Switch(active = False )
  
           # Add widget
           self .add_widget( self .settings_sample)
  
           # Arranging a callback to the switch
           # using bing function
           self .settings_sample.bind(active = switch_callback)       
  
# Callback for the switch state transition
# Defining a Callback function
# Contains Two parameter switchObject, switchValue
def switch_callback(switchObject, switchValue):
      
     # Switch value are True and False
     if (switchValue):
         print ( 'Switch is ON:):):)' )
     else :
         print ( 'Switch is OFF:(:(:(' )
  
   
# Defining the App Class
class SwitchApp(App):
      # define build function
      def build( self ):
           # retuen the switch class
           return SimpleSwitch()
  
   
# Run the kivy app
if __name__ = = '__main__' :
      SwitchApp().run()

输出如下:

Python |在Kivy中切换小部件3

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


赞(0) 打赏
未经允许不得转载:srcmini » Python如何在Kivy中切换小部件?
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

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

微信扫一扫打赏