Kivy 中的 Python 複選框小部件?


Kivy 是一款開源 Python 庫,用於快速開發應用程式,這些應用程式使用創新的使用者介面(例如多點觸碰應用程式)。它用於開發 Android 應用程式以及桌面應用程式。在本文中,我們將瞭解如何使用 GridLayout 和複選框。

在匯入相關模組後,我們建立了一個帶有 2 列的網格佈局。一欄用於容納標籤,另一欄用於容納複選框。

例項

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox
from kivy.uix.gridlayout import GridLayout

# Container class for the app's widgets
class chk_box(GridLayout):

   def __init__(self, **kwargs):
      super(chk_box, self).__init__(**kwargs)

      # Grid layout for 2 columns
      self.cols = 2

      # Add checkbox, widget and labels
      self.add_widget(Label(text='10 AM to 11 AM' ))
      self.active = CheckBox(active=True)
      self.add_widget(self.active)

      self.add_widget(Label(text='3 PM to 4 PM'))
      self.active = CheckBox(active=False)
      self.add_widget(self.active)

class CheckBoxApp(App):
   def build(self):
      return chk_box()

CheckBoxApp().run()

輸出

執行以上程式碼,會得到如下結果 −

更新日期:2021-01-12

486 次瀏覽

啟動您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.