Kivy 简明教程

Kivy - Checkbox

在任何 GUI 工具包中,都使用复选框允许用户从可用选项中选择一个或多个。在 Kivy 中,CheckBox 可以配置为使选择互斥(仅可选择可用选项之一),或者允许用户标记任意数量的选择。

In any GUI toolkit, a checkbox is used to enable the user to select one or choices from the available options. In Kivy, the CheckBox can be configured to make the choice mutually exclusive (only one of the available options is selectable), or let the user mark any number of choices.

  1. If two or more checkboxes have the same value for group property, they appear as a circular radiobutton; user can choose only one option, as the active property of only one checkbox can be True, for others active property will be automatically False.

  2. For the checkboxes not having the group property, it appears as a rectangular box which when pressed, shows a checkmark with active property becoming True. Click it again and the check marks is removed, the active property becomes False.

CheckBox 类在 kivy.uix.checkbox 模块中定义

The CheckBox class is defined in kivy.uix.checkbox module

from kivy.uix.checkbox import CheckBox
cb = CheckBox(**kwargs)

如果复选框对象绑定到其 active 属性,则每次 active 属性更改时都将调用回调。

If the checkbox object is be bound to its active property, a callback can be invoked every time the active property changes.

checkbox = CheckBox()
checkbox.bind(active=callback)

Example

以下 Python 代码展示了如何使用互斥以及多选复选框。

The following Python code shows how to use checkboxes that are mutually exclusive, as well as multi-selectable.

代码使用垂直 BoxLayout,其中包含两个水平布局和两个标签。上水平布局保留两个复选框,其 group 属性均为“sex”

The code uses a vertical BoxLayout with two horizontal layouts and two labels in it. The upper horizontal layout holds two checkboxes with group property of both as 'sex'

self.m = CheckBox(group='sex', color=[1,0,1,1])
self.m.bind(active=self.on_male)
gendergrp.add_widget(self.m)
gendergrp.add_widget(Label(text='Female'))
self.f = CheckBox(active=False, group='sex')
self.f.bind(active=self.on_female)
gendergrp.add_widget(self.f)

这两个复选框都调用一个回调方法,该方法标识 active 属性。

Both the checkboxes invoke a callback method that identifies the active property.

下水平框保留三个独立复选框 −

The lower horizontal box holds three independent checkboxes −

interests.add_widget(Label(text='Sports'))
self.cb1 = CheckBox()
self.cb1.bind(active=self.on_interest)
interests.add_widget(self.cb1)

self.cb2 = CheckBox()
self.cb2.bind(active=self.on_interest)
interests.add_widget(Label(text='Music'))
interests.add_widget(self.cb2)

self.cb3 = CheckBox()
self.cb3.bind(active=self.on_interest)
interests.add_widget(Label(text='Travel'))
interests.add_widget(self.cb3)

单击这些复选框中的每一个时,都会建立和显示水平框下面标签上的已选择兴趣列表。

When each of these checkboxes are clicked, a list of selected interests is built and displayed on the label below the horizontal box.

这是 complete code

Here is the complete code

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

Window.size = (720,400)
class CheckBoxApp(App):
   gender=''
   intrst=[]
   def on_male(self, instance, value):
      if value:
         CheckBoxApp.gender='Male'
         self.lbl.text = "Gender selected: "+CheckBoxApp.gender
      else:
         self.lbl.text = "Gender selected: "
   def on_female(self, instance, value):
      if value:
         CheckBoxApp.gender='Female'
         self.lbl.text = "Gender selected: "+CheckBoxApp.gender
      else:
         self.lbl.text = "Gender selected: "
   def on_interest(self, instance, value):
      CheckBoxApp.intrst=[]
      if self.cb1.active:
         CheckBoxApp.intrst.append("Sports")
      if self.cb2.active:
         CheckBoxApp.intrst.append("Music")
      if self.cb3.active:
         CheckBoxApp.intrst.append("Travel")
      self.lbl1.text="Interests Selected: "+" ".join(CheckBoxApp.intrst)

   def build(self):
      main=BoxLayout(orientation='vertical')

      gendergrp=BoxLayout(orientation='horizontal')
      interests = BoxLayout(orientation='horizontal')

      gendergrp.add_widget(Label(text='Gender:'))
      gendergrp.add_widget(Label(text='Male'))
      self.m = CheckBox(group='sex', color=[1,0,1,1])
      self.m.bind(active=self.on_male)
      gendergrp.add_widget(self.m)
      gendergrp.add_widget(Label(text='Female'))
      self.f = CheckBox(active=False, group='sex')
      self.f.bind(active=self.on_female)

      gendergrp.add_widget(self.f)
      main.add_widget(gendergrp)
      self.lbl = Label(text="Gender selected: ", font_size=32)

      main.add_widget(self.lbl)

      interests.add_widget(Label(text='Interests:'))
      interests.add_widget(Label(text='Sports'))
      self.cb1 = CheckBox()
      self.cb1.bind(active=self.on_interest)
      interests.add_widget(self.cb1)

      self.cb2 = CheckBox()
      self.cb2.bind(active=self.on_interest)
      interests.add_widget(Label(text='Music'))
      interests.add_widget(self.cb2)

      self.cb3 = CheckBox()
      self.cb3.bind(active=self.on_interest)
      interests.add_widget(Label(text='Travel'))
      interests.add_widget(self.cb3)

      self.lbl1 = Label(text="Interests selected: ", font_size=32)
      main.add_widget(interests)
      main.add_widget(self.lbl1)

      return main

if __name__ == '__main__':
   CheckBoxApp().run()

Output

运行此代码时,它将生成如下所示的 GUI −

When you run this code, it will produce a GUI like the one shown here −

kivy checkbox