Kivy - 複選框



在任何GUI工具包中,複選框用於允許使用者從可用選項中選擇一個或多個選項。在Kivy中,可以配置CheckBox使其選擇互斥(只有一個可用選項可選),或者允許使用者標記任意數量的選項。

  • 如果兩個或多個複選框的group屬性值相同,則它們顯示為圓形單選按鈕;使用者只能選擇一個選項,因為只有一個複選框的active屬性可以為True,其他複選框的active屬性將自動變為False。

  • 對於沒有group屬性的複選框,它顯示為一個矩形框,按下時會顯示一個勾號,active屬性變為True。再次點選它,勾號將被移除,active屬性變為False。

CheckBox類在kivy.uix.checkbox模組中定義。

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

如果複選框物件與其active屬性繫結,則每次active屬性更改時都會呼叫一個回撥函式。

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

示例

以下Python程式碼演示瞭如何使用互斥的複選框以及多選複選框。

程式碼使用一個垂直BoxLayout,其中包含兩個水平佈局和兩個標籤。上方的水平佈局包含兩個group屬性都為'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屬性。

下方的水平盒子包含三個獨立的複選框。

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)

單擊這些複選框中的每一個時,都會構建一個選定興趣列表並在下面的標籤中顯示。

這是完整程式碼

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()

輸出

執行此程式碼後,它將生成如下所示的GUI:

Kivy Checkbox
廣告
© . All rights reserved.