Skip to main content
 首页 » 编程设计

python之获取kivy中选中复选框的值

2025年05月04日18pengyingh

测试.py

import sqlite3 as lite 
 
from kivy.uix.screenmanager import Screen 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.core.window import Window 
 
Window.size = (600, 325) 
 
class UserGroup(Screen): 
 
    def insert_data(self, arg1,arg2): 
        print(arg1) 
        print(arg2) 
 
 
class FactUserGroup(App): 
 
    def build(self): 
        self.root = Builder.load_file('test.kv') 
        return self.root 
 
 
if __name__ == '__main__': 
    FactUserGroup().run() 

测试.kv

<CustomLabel@Label>: 
    text_size: self.size 
    valign: "middle" 
    padding_x: 5 
 
<SingleLineTextInput@TextInput>: 
    multiline: False 
 
<GreenButton@Button>: 
    background_color: 1, 1, 1, 1 
    size_hint_y: None 
    height: self.parent.height * 0.120 
 
UserGroup 
 
    GridLayout: 
        cols: 2 
        padding : 30,30 
        spacing: 20, 20 
        row_default_height: '30dp' 
 
        Label: 
            text: 'Male' 
            text_size: self.size 
            valign: 'middle' 
 
        CheckBox: 
            group: 'check' 
            id : chk 
 
 
        Label: 
            text: 'Female' 
            text_size: self.size 
            valign: 'middle' 
 
        CheckBox: 
            group: 'check' 
 
        CustomLabel: 
            text: 'age' 
            text_size: self.size 
            valign: 'middle' 
 
        SingleLineTextInput: 
            id: age 
 
 
        GreenButton: 
            text: 'Ok' 
            on_press: root.insert_data(chk.text,age.text) 
 
 
        GreenButton: 
            text: 'Cancel' 
            on_press: app.stop() 

如何获取复选框的值?我正在使用 age.text 获取年龄文本框的值,但我不知道复选框的值?
单击“确定”时,如何获取选中的复选框值并传入 root.insert_data。

请您参考如下方法:

您可以通过其 active 属性获取复选框的选中状态,因此请尝试更改:

GreenButton: 
    text: 'Ok' 
    on_press: root.insert_data(chk.active ,age.text) 

在此代码段中,chk.text 已更改为 chk.active,这对我来说很合适。

在 https://kivy.org/docs/api-kivy.uix.checkbox.html 查看更多关于 kivy 复选框的引用

希望对您有所帮助。试一试。

更新:

因此,为了能够获取每个复选框的属性和文本输入,您可以将 ObjectProperties 分配给小部件,并且您可以将它们链接到您的 test.py 文件。

修改后的来源:

测试.py
from kivy.uix.screenmanager import Screen 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.core.window import Window 
from kivy.properties import ObjectProperty 
 
Window.size = (600, 325) 
 
class UserGroup(Screen): 
    male = ObjectProperty(None) 
    female = ObjectProperty(None) 
    age = ObjectProperty(None) 
     
    def insert_data(self): 
        if self.male.active: 
            print('Male') 
        elif self.female.active: 
            print('Female') 
        else: 
            print('No gender selected') 
        print(self.age.text) 
 
 
class FactUserGroup(App): 
 
    def build(self): 
        self.root = Builder.load_file('test.kv') 
        return self.root 
 
 
if __name__ == '__main__': 
    FactUserGroup().run() 

在 .py 文件中,您可以找到新导入的 ObjectProperty。 您还可以看到在 UserGroup 中定义了三个新属性以与 View 交互,并且在 UserGroup.insert_data 中的修改非常简单。

测试.kv

<CustomLabel@Label>: 
    text_size: self.size 
    valign: "middle" 
    padding_x: 5 
 
<SingleLineTextInput@TextInput>: 
    multiline: False 
 
<GreenButton@Button>: 
    background_color: 1, 1, 1, 1 
    size_hint_y: None 
    height: self.parent.height * 0.120 
 
UserGroup 
 
    male: chk_male 
    female: chk_female 
    age: txt_age 
 
    GridLayout: 
        cols: 2 
        padding : 30,30 
        spacing: 20, 20 
        row_default_height: '30dp' 
 
        Label: 
            text: 'Male' 
            text_size: self.size 
            valign: 'middle' 
 
        CheckBox: 
            group: 'check' 
            id : chk_male 
 
        Label: 
            text: 'Female' 
            text_size: self.size 
            valign: 'middle' 
 
        CheckBox: 
            group: 'check' 
            id: chk_female 
 
        CustomLabel: 
            text: 'age' 
            text_size: self.size 
            valign: 'middle' 
 
        SingleLineTextInput: 
            id: txt_age 
 
 
        GreenButton: 
            text: 'Ok' 
            on_press: root.insert_data() 
 
 
        GreenButton: 
            text: 'Cancel' 
            on_press: app.stop() 

在 .kv 文件中,两个复选框的 id 和文本输入被重命名为 chk_male、chk_female 和 txt_age 分别。

您还可以看到对象属性链接定义在 UserGroup 部分的开头。

希望它有意义并符合您的要求。