get value of selected checkbox in kivy

爷,独闯天下 提交于 2019-12-06 16:41:06

You can get the checked state of a checkbox with its active property, so try change:

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

In this snippet chk.text was changed to chk.active which works for me properly.

See more reference on kivy checkboxes at https://kivy.org/docs/api-kivy.uix.checkbox.html

Hope it helps. Give it a try.

UPDATE:

So in order to be able to get the properties of each checkbox and the text input you can assign ObjectProperties to the widgets, and you can link them to your test.py file.

The modified sources:

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

In the .py file you can find a new import of ObjectProperty. Also you can see that three new properties were defined in UserGroup to interact with the view, and the modifications in UserGroup.insert_data are straightforward.

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

In the .kv file the ids of the two checkboxes and the text input are renamed to chk_male, chk_female and txt_age respectively.

Also you can see that the object property links are defined at the beginning of the UserGroup section.

Hope it makes sense and match your requirements.

The solution is to use StringProperty, add text (Male, Female) to CheckBox, and on_active event to capture the gender.

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)

Example

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

Output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!