Kivy小部件的不透明度不会在视觉上更新

时间:2019-08-30 11:10:01

标签: python-3.x kivy kivy-language

我想隐藏一个小部件,并在单击按钮时显示另一个小部件。我是通过将不透明度形式从1更改为0,从0更改为1来实现的。firt小部件变为不可见,但第二个未显示。
这是我的python文件:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty


from kivy.config import Config
Config.set('graphics', 'width', '1920')

Config.set('graphics', 'height', '1080')
Config.set('graphics', 'fullscreen', 1)



class FirstScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class MyScreenManager(ScreenManager):
    pass

class PasswordEntryWidget(BoxLayout):
    def checkPswd(instance, pswd):
        if (pswd == "123"):
            instance.parent.opacity = 0
            instance.parent.disabled = True
            instance.parent.parent.ids.settingsWgt.opacity = 1.0
            #print(instance.parent.parent.ids.settingsWgt.size_hint)

class SettingsEntryWidget(BoxLayout):
    def checkPswd(instance, pswd):
        if (pswd == "123"):
            print ("It's OK!")

class MyApp(App):
    def build(self):
        return MyScreenManager()

if __name__ == '__main__':
    MyApp().run()

这是my.kv:

#:kivy 1.11.1
#:include loginWidgets.kv
<MyScreenManager>:
    FirstScreen:
    SecondScreen:

<FirstScreen>:
    name: "FirstScreen"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'img/background.png'

    FloatLayout:
        size: root.size
        PasswordEntryWidget:
            id: passwordWgt
            size_hint: 0.2, 0.1
            pos_hint: {'top': 0.35, 'right': 0.7}
        SettingsEntryWidget:
            id: settingsWgt 
            opacity: 0
            disabled: True
            size_hint: 0.2, 0.32
            pos_hint: {'top': 0.35, 'right': 0.95}



<SecondScreen>:
    name: "SecondScreen"
    Label:
        color: 1,1,1,1
        font_size: 10
        center_x: root.width/4
        top: root.top - 50
        text: "Lalalala"

这是loginWidgets.kv:

#:kivy 1.11.1

<PasswordEntryWidget>:
    orientation: "vertical"
    spacing: 20
    #padding: 50
    TextInput:
        id: adminPswdLine
        multiline: False
        password: True
        size_hint_y: None
        height: 30
        font_size: self.height - 10
    Button:
        id: "takeAdminPswd"
        size_hint_x: 0.5
        pos_hint: {'center_x': 0.5}
        on_press: root.checkPswd(adminPswdLine.text)

<SettingsEntryWidget>:
    orientation: "vertical"
    spacing: 20
    #padding: 50
    TextInput:
        id: host
        padding: [5, 2, 0, 5]
        text: ""
        multiline: False
        password: False
        size_hint_y: None
        height: 30
        font_size: self.height - 10
    Button:
        id: "takeSettings"
        size_hint_x: 0.5
        pos_hint: {'center_x': 0.5}
        on_press: root.checkPswd(adminPswdLine.text)

正如我所说,第一部分效果很好。 PasswordWgt可以完美隐藏,但是SettingsWgt不会显示。当我打印后不透明-它给出1,因此小部件必须可见,但不可见。我检查了id,那里有哪些元素以及所需的小部件是否有这个id。
是错误还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是您要更改容器(FloatLayout而不是PasswordEntryWidget的不透明度。只需更改:

class PasswordEntryWidget(BoxLayout):
    def checkPswd(instance, pswd):
        if (pswd == "123"):
            instance.parent.opacity = 0
            instance.parent.disabled = True
            instance.parent.parent.ids.settingsWgt.opacity = 1.0

收件人:

class PasswordEntryWidget(BoxLayout):
    def checkPswd(instance, pswd):
        if (pswd == "123"):
            instance.opacity = 0
            instance.disabled = True
            instance.parent.parent.ids.settingsWgt.opacity = 1.0

我认为它会按您的意愿工作。