获取kivy中所选复选框的值

时间:2017-11-20 10:19:10

标签: python python-2.7 kivy kivy-language

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获取年龄文本框的值,但我不知道复选框值?
单击“确定”,然后单击“如何获取选中”复选框值并传入root.insert_data。

2 个答案:

答案 0 :(得分:3)

您可以使用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文件。

修改后的来源:

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

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

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

答案 1 :(得分:2)

解决方案是使用 StringProperty ,将 文本 (男,女)添加到CheckBox,然后< strong> 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()

输出

Figure 1 - Male & Age 21 Figure 2 - Female & Age 20

相关问题