如何隐藏基于kivy复选框的TextBox

时间:2017-11-20 07:52:16

标签: 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):
    pass


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'


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

我是python / kivy的新手。我不知道复选框是如何工作的?

如何隐藏,显示'年龄'文本框,根据男性,女性复选框标签 如果我选择男性,则应显示年龄文本框和标签。如果我选​​择女性,则应删除年龄文本框和标签。

1 个答案:

答案 0 :(得分:2)

解决方案是使用 不透明度 属性来隐藏窗口小部件。默认情况下,不透明度设置为 1 表示可见, 0 表示不可见。我还使用ObjectProperty来连接年龄Label小部件和TextInput小部件。有关详细信息,请参阅示例。

widget.opacity = 0    # invisible

实施例

test.py

import sqlite3 as lite

from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window

Window.size = (600, 325)


class UserGroup(Screen):
    age_label = ObjectProperty(None)
    age_textinput = ObjectProperty(None)


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:
    age_label: age_label
    age_textinput: age

    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 20, 20
        row_default_height: '30dp'

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

        CheckBox:
            id : chk
            group: 'check'
            on_active:
                root.age_label.opacity=1    # Visible
                root.age_textinput.opacity=1    # Visible

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

        CheckBox:
            group: 'check'
            on_active:
                root.age_label.opacity=0    # Invisible
                root.age_textinput.opacity=0    # Invisible

        CustomLabel:
            id: age_label
            text: 'age'
            text_size: self.size
            valign: 'middle'

        SingleLineTextInput:
            id: age


        GreenButton:
            text: 'Ok'


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

输出

Figure 1 - App Startup Figure 2 - CheckBox Female Checked & Age Widgets Invisible Figure 3 - CheckBox Male Checked & Age Widgets Visible