Python&Kivy显示/隐藏框

2024-09-23 16:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我建立了一个应用程序,这是拍照,去另一个空屏幕上运行我的主要代码的背景。当我的主代码处于第一个if条件时,我想在空屏幕上显示一个文本输入框;当代码处于第二个if条件时,我想隐藏这个框。我的代码在下面。我写了“废话”来回答我的问题。你知道吗

class CheckScreen(Screen):
    def deneme(self):

    #MY MAIN CODE
    #...

        if(BLABLABLA)
            self.isShownMenu = BooleanProperty(True)
        else
            self.isShownMenu = BooleanProperty(False)

GUI = Builder.load_string("""

#BLABLABLA1
#...

<SingleLineTextInput@TextInput>:
    pos_hint: {'center_x': .5, 'center_y': .4}
    size_hint: 0.5, 0.05
    multiline: False
<CheckScreen>:

    #BLABLABLA2
    #...

    SingleLineTextInput:
        opacity: 1 if root.isShownMenu else 0
""")

class TestCamera(App):

def build(self):
    return GUI

TestCamera().run()

当我运行这个程序时,即使我在条件中将True改为False,应用程序也总是显示文本输入。我的问题在哪里?你知道吗


Tags: 代码文本selffalsetrue应用程序if屏幕
1条回答
网友
1楼 · 发布于 2024-09-23 16:33:19

您的布尔属性需要在类级别定义:

class CheckScreen(Screen):
    isShownMenu = BooleanProperty(True)

根据需要使用TrueFalse。然后在代码中只引用self.isShownMenu,如:

    if(BLABLABLA)
        self.isShownMenu = True
    else
        self.isShownMenu = False

相关问题 更多 >