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


测试文件

<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文件。

修改后的来源:

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中的修改非常简单。

测试文件

<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部分的开头定义了对象属性链接。

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

关于python - 获取 kivy 中所选复选框的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47389711/

10-09 12:31