获取kivy中选中复选框的值

get value of selected checkbox in kivy

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

test.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 获取年龄文本框的值,但我不知道复选框的值?
当点击 'Ok' 然后如何获取选中的复选框值并传入 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 分配给小部件,并且您可以将它们 link 分配给您的 test.py文件。

修改来源:

test.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 中定义了三个新属性以与视图进行交互,并且 UserGroup.insert_data 中的修改非常简单。

test.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_malechk_femaletxt_age

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

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

解决方法是使用StringProperty,添加text (Male, Female) 到 CheckBox,和 on_active 事件来捕获性别。

test.py

from kivy.properties import StringProperty
...
    def insert_data(self, age):
        print("Gender={}".format(self.gender))
        print("Age={}".format(age))

test.kv

        CheckBox:
            group: 'check'
            id : chk
            text: "Male"
            on_active:
                root.gender = self.text
        ...
        CheckBox:
            group: 'check'
            text: "Female"
            on_active:
                root.gender = self.text
        ...
        GreenButton:
            text: 'Ok'
            on_press: root.insert_data(age.text)

例子

test.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
from kivy.properties import StringProperty

Window.size = (600, 325)


class UserGroup(Screen):
    gender = StringProperty("")

    def insert_data(self, age):
        print("Gender={}".format(self.gender))
        print("Age={}".format(age))


class FactUserGroup(App):

    def build(self):
        self.root = Builder.load_file('test.kv')
        return self.root


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

test.kv

#:kivy 1.10.0

<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
            text: "Male"
            on_active:
                root.gender = self.text

        Label:
            text: 'Female'
            text_size: self.size
            valign: 'middle'

        CheckBox:
            group: 'check'
            text: "Female"
            on_active:
                root.gender = self.text

        CustomLabel:
            text: 'age'
            text_size: self.size
            valign: 'middle'

        SingleLineTextInput:
            id: age

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

        GreenButton:
            text: 'Cancel'
            on_press: app.stop()

输出