获取猕猴桃中所选复选框的值 [英] get value of selected checkbox in kivy

查看:48
本文介绍了获取猕猴桃中所选复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

How to get value of checkbox?I am getting value of age textbox using age.text but checkbox value i don't know?
When click on 'Ok' then How to get selected checkbox value and pass in root.insert_data.

推荐答案

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

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)

在此代码段中,chk.text更改为chk.active,对我来说正常工作.

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

https://kivy.org/docs上查看有关kivy复选框的更多参考/api-kivy.uix.checkbox.html

希望它会有所帮助.试试看.

Hope it helps. Give it a try.

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

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.

修改后的来源:

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中的修改很简单.

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.

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

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.

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

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.

这篇关于获取猕猴桃中所选复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆