在弹出窗口中添加多个按钮

Posted

技术标签:

【中文标题】在弹出窗口中添加多个按钮【英文标题】:adding multiple buttons in a popup window 【发布时间】:2019-11-12 10:40:21 【问题描述】:

我创建了一个弹出窗口。通常我们使用 pop.dismiss 来关闭弹窗。但我想在该弹出窗口中添加一些按钮。我有 4 个按钮。当按下这些按钮中的 2 个时,它们应该显示另一个小部件(boxlayout)。 但是当我触摸这些按钮时,应用程序崩溃了。

但是,这 4 个按钮中的其他 2 个在触摸时会显示另一个弹出窗口。它运行良好而不会崩溃。

    从弹出窗口>按钮触摸>显示另一个弹出窗口>没有崩溃 从弹出窗口>按钮这 4 个按钮中的任何一个> 以显示 boxlayout 小部件> 应用程序崩溃了!

谁能解释一下?我应该如何解决这个问题?

(.py) 文件

    class abc(Popup):   

        def about_app(self):
            self.clear_widgets()
            self.add_widget(about())    

        def about_leo(self):
            self.clear_widgets()
            self.add_widget(page1())

        def help(self):
            pops=help_popup()
            pops.open() 

        def website(self):
            pops=website()
            pops.open()

(.kv) 文件

<abc>:  
    title: 'LEO CLUB'
    title_color: 1, 0, 0, 1 
    title_size: 50
    title_align:'center'
    background: 'popup.png'
    size_hint: .6, 0.8
    pos_hint: 'right': .6, 'top': 1

    BoxLayout:          
        BoxLayout:
            orientation:'vertical'                      

            Button:
                bold: True
                text: "About LEO"
                background_color: 0, 0, 0, 0
                on_release: root.about_leo()
            Button:
                bold:True
                text: "About App"
                background_color: 0, 0, 0, 0
                on_release: root.about_app()                                                 
            Button:
                bold: True
                text: "Website"
                background_color: 0, 0, 0, 0
                on_release: root.website()
            Button:
                bold: True
                text: "Help"
                background_color: 0, 0, 0, 0
                on_release: root.help()

【问题讨论】:

请发minimal reproducible example。 您是要添加我的代码吗? 我做到了。现在请帮助我 【参考方案1】:

您的代码在abc 类(即Popup)中调用self.add_widget(),但Popup 只能有一个孩子(即content)。对clear_widgets() 的调用会删除Popup 的所有子项,但不会更改content 属性(它可能应该)。所以即使你删除了Popupchildren,它仍然认为它有一个非空的content。所以,你真正需要做的只是设置新的content。在您的 abc 类中,只需将这两个方法替换为:

def about_app(self):
    self.content = about()

def about_leo(self):
    self.content = page1()

【讨论】:

确实比以前好一点。但是,通过调用 self.content=page1(),它只是在该弹出窗口中显示 page1。它没有清除弹出窗口下方的基本窗口,由于透明度仍然可见,并且弹出窗口的大小为(.5,.5)。因此,当我调用 page1() 时,它会在该弹出窗口中显示 page1,大小约为 (0.5,0.5)。之后,如果我的 page1 有一个按钮可以重定向到另一个页面,并且当我触摸 page1 的那个按钮时,它会在该弹出窗口中再次显示另一个窗口,而不会清除下面的可见透明窗口。 这就是我们要求minimal reproducible example 的原因。一定有其他东西(不在您的帖子中)影响了Popup 的透明度。如果您正在设置Popupopacity 属性,或者popup.png 具有一定的透明度,则有一些可能性。默认Popup 不透明。 你好,约翰,我确实通过在互联网上进行了大量研究和我的努力解决了我的问题。谢谢你的帮助

以上是关于在弹出窗口中添加多个按钮的主要内容,如果未能解决你的问题,请参考以下文章