【问题标题】:Kivy widget's opacity doesn't update visuallyKivy 小部件的不透明度不会在视觉上更新
【发布时间】:2019-08-30 22:03:22
【问题描述】:

我想隐藏一个小部件并在单击按钮时显示另一个小部件。我通过将不透明度从 1 更改为 0 并从 0 更改为 1 来做到这一点。第一个小部件变得不可见,但第二个小部件没有显示。
这是我的 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()

这是我的.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 一起存在。
是错误还是我做错了什么?

【问题讨论】:

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


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

    我认为它会如你所愿。

    【讨论】:

    • 第一个小部件变得不可见,所以instance.parent.opacity = 0 就像我需要的那样工作。问题出在此处:`instance.parent.parent.ids.settingsWgt.opacity = 1.0`,但instance.parent.parent.ids 中有具有此类 id 的小部件
    • 但是使整个容器不透明度 = 0 意味着您将SettingsEntryWidget 的不透明度设置为无关紧要,因为它的容器(父级)的不透明度 = 0。如果容器不透明度是零,它的孩子都不会出现。请尝试我的答案。
    • 嗯...这是有道理的...这行得通!哦,天哪,那么instance 是什么?是按钮吗?那么为什么 button.opacity 会改变所有小部件的不透明度呢?或者它是小部件?但为什么会这样?那么为什么 instance.parent.parent 在 id 中有 settingsWgt 呢?
    • 'instance 在您的checkPswd() 方法中通常命名为self(但这不是必需的),其值为PasswordEntryWidget 实例。 PasswordEntryWidgetparent.parentFirstScreen 实例(其中包含“ids”字典)。
    • 非常感谢!很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2020-08-26
    • 1970-01-01
    • 2012-01-24
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多