Kivy / python:如何检查.py文件中的复选框

时间:2017-11-22 12:28:53

标签: python python-2.7 kivy kivy-language

我已经用Python(test.py)和kivy(test.kv)编写了一些代码。 当我运行test.py时,男性复选框显示已选中且女性复选框未选中,因为我在test.kv文件中使用:

active: root.male

但我希望从.py文件中获得同样的东西。如何从.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):
        print('')


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

    male: chk_male
    female: chk_female

    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
            active: root.male

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

        CheckBox:
            group: 'check'
            id: chk_female


        GreenButton:
            text: 'Ok'


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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

解决方案是使用 BooleanProperty ,然后添加 active:root.female 。在示例中,它说明了Kivy应用程序何时运行,女性的复选框处于活动状态(图1),5秒后,它将使用Clock.schedule_once自动切换到男性复选框(图2)。

test.py

from kivy.properties import ObjectProperty, BooleanProperty
...
class UserGroup(Screen):
    male = BooleanProperty(False)
    female = BooleanProperty(True)

test.kv

    CheckBox:
        group: 'check'
        id: chk_female
        active: root.female

实施例

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, BooleanProperty
from kivy.clock import Clock

Window.size = (600, 325)


class UserGroup(Screen):
    male = BooleanProperty(False)
    female = BooleanProperty(True)
    age = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(UserGroup, self).__init__(**kwargs)
        Clock.schedule_once(self.switch_checkbox, 5)

    def switch_checkbox(self, dt):
        self.female = False
        self.male = True

    def insert_data(self):
        print('')


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_male
            active: root.male

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

        CheckBox:
            group: 'check'
            id: chk_female
            active: root.female


        GreenButton:
            text: 'Ok'


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

输出

Figure 1 - CheckBox Female Figure 2 - CheckBox Male after 5 minutes