Python|使用 .kv 文件创建复选框
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、Linux、Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
Kivy Tutorial – Learn Kivy with Examples.
复选框小部件:
CheckBox 是一个特定的两种状态按钮,可以选中或取消选中。
要使用复选框,您首先必须从包含复选框的所有特性和功能的模块中导入复选框,即
from kivy.uix.checkbox import CheckBox
Basic Approach to follow while creating Checkbox using .kv file :
1) import kivy
2) import kivyApp
3) import BoxLayout
4) import Checkbox
5) set minimum version(optional)
6) Extend the container class
7) set up .kv file :
9) Return layout
10) Run an instance of the class
现在是如何使用 .kv 文件在 Kivy 中创建复选框的程序:
Python3
# main.py file
# program for creating checkbox using .kv in kivy.
# import kivy module
import kivy
# set require version
kivy.require("1.9.0")
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
## not necessary while using .kv file
from kivy.uix.checkbox import CheckBox
# To do some manipulation on window import window
from kivy.core.window import Window
# Container class for the app's widgets
class SampBoxLayout(BoxLayout):
# Callback for the checkbox
def checkbox_click(self, instance, value):
if value is True:
print("Checkbox Checked")
else:
print("Checkbox Unchecked")
# App derived from App class
class SampleApp(App):
# build is a method of Kivy's App class used
# to place widgets onto the GUI.
def build(self):
# setting up window background color
Window.clearcolor = (0, 0, .30, .60)
return SampBoxLayout()
# Run the app
root = SampleApp()
root.run()
Python3
#.kv file of main.py file
#: import CheckBox kivy.uix.checkbox
# giving colour to label
:
color: .761, .190, .810, 1
:
orientation: "vertical"
padding: 10
spacing: 10
CustLabel:
text: "Gender"
size_hint_x: 1
font_size:20
# creating box layout
BoxLayout:
# assigning orientation
orientation: "horizontal"
height: 20
BoxLayout:
orientation: "horizontal"
size_hint_x: .22
# label creation
CustLabel:
text: "Male"
size_hint_x: .80
font_size:30
CheckBox:
color:.294, .761, .623
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
CustLabel:
text: "Female"
size_hint_x: .80
font_size:20
CheckBox:
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
CustLabel:
text: "Other"
size_hint_x: .80
font_size:10
CheckBox:
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
代码的 sample.kv 文件。
Python3
#.kv file of main.py file
#: import CheckBox kivy.uix.checkbox
# giving colour to label
:
color: .761, .190, .810, 1
:
orientation: "vertical"
padding: 10
spacing: 10
CustLabel:
text: "Gender"
size_hint_x: 1
font_size:20
# creating box layout
BoxLayout:
# assigning orientation
orientation: "horizontal"
height: 20
BoxLayout:
orientation: "horizontal"
size_hint_x: .22
# label creation
CustLabel:
text: "Male"
size_hint_x: .80
font_size:30
CheckBox:
color:.294, .761, .623
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
CustLabel:
text: "Female"
size_hint_x: .80
font_size:20
CheckBox:
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
CustLabel:
text: "Other"
size_hint_x: .80
font_size:10
CheckBox:
on_active: root.checkbox_click(self, self.active)
size_hint_x: .20
输出:
视频输出: