【问题标题】:Kivy/python : How to check checkbox from a .py fileKivy/python:如何从 .py 文件中检查复选框
【发布时间】:2017-11-22 12:28:52
【问题描述】:

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

有人可以帮我吗?

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案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()
    

    输出

    【讨论】:

    • 非常感谢。我真的很抱歉我不能投票,因为我的声望低于 15。当我超过 15 声望时,我会投票。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2021-09-18
    • 2011-01-27
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多