📜  Python|在 Kivy 中切换小部件

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

Python|在 Kivy 中切换小部件

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

切换小部件:

Switch 小部件作为机械灯开关处于活动或非活动状态。用户可以向左/向右滑动以激活/停用它。开关表示的值是 True 或 False。也就是说,开关可以处于打开位置或关闭位置。
要使用 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

方法的实施:

Python3
# 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 columns
          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):
          # return the switch class
          return SimpleSwitch()
 
  
# Run the kivy app
if __name__ == '__main__':
     SwitchApp().run()


Python3
# 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 columns
          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):
          # return the switch class
          return SimpleSwitch()
 
  
# Run the kivy app
if __name__ == '__main__':
     SwitchApp().run()


输出:

将回调附加到交换机:

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

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

Python3

# 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 columns
          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):
          # return the switch class
          return SimpleSwitch()
 
  
# Run the kivy app
if __name__ == '__main__':
     SwitchApp().run()

输出: