在 Kivy 中,如何使用另一个类的关闭按钮关闭弹出窗口?

Posted

技术标签:

【中文标题】在 Kivy 中,如何使用另一个类的关闭按钮关闭弹出窗口?【英文标题】:In Kivy, how do I close a Popup using a close button from another class? 【发布时间】:2019-12-29 15:45:44 【问题描述】:

我需要使用关闭按钮关闭 kivy 中的弹出窗口。

我在这里找到了一些解决方案,但它与我当前使用的 ScreenManager 不兼容。为了显示我想要的弹出窗口,我使用 FloatLayout 并将其作为弹出窗口的内容传递。 当我使用函数 close 时,它​​会在 de FloatLayout 类内部调用并且不起作用。那么如何关闭 MainWindow 中的弹出窗口呢?

这是我的代码:

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.floatlayout import FloatLayout

    class MainWindow(Screen):
        def open(self):
            pop = Pop()
            popup = Popup(title="",
                          content=pop,
                          size_hint=(.8, .8))
            popup.open()

    class Pop(FloatLayout):
        def close(self):
            self.dismiss()

    class Setting(Screen):
        pass

    class WindowManager(ScreenManager):
        pass

    kv = Builder.load_file("teste.kv")

    class TesteApp(App):
        def build(self):
            return kv

    if __name__ == "__main__":
        TesteApp().run()

对于我的 kv 文件:

    #:import NoTransition kivy.uix.screenmanager.NoTransition
    #:import SlideTransition kivy.uix.screenmanager.SlideTransition

    WindowManager:
        MainWindow:
        Setting:


    <MainWindow>:
        name: "main"

        FloatLayout:
            Label:
                pos_hint:'center_x': .5, 'center_y': .8
                size_hint:0.5, 0.5
                text: "TITLE"
                font_size: (root.width/30 + root.height/30)

            Button:
                pos_hint:'center_x': .5, 'center_y': .4
                size_hint:0.6, 0.1
                text: "Set"
                on_release:
                    app.root.transition = SlideTransition(direction='left')
                    app.root.current = "setting"

            Button:
                pos_hint:'center_x': .5, 'center_y': .25
                size_hint:0.6,0.1
                text: "Pop"
                on_release:
                    root.open()


    <Setting>:
        name: "setting"

        FloatLayout:
            Label:
                text: 'Set Time'
                pos_hint:'center_x': .5, 'center_y': .75
                size_hint: 0.1, 0.1
                font_size: (root.width/30 + root.height/30)

            Button:
                pos_hint:'center_x': .1, 'center_y': .1
                size_hint:0.05,0.05
                on_release:
                    app.root.transition = SlideTransition(direction='right')
                    app.root.current = 'main'

    <Pop>:
        Label:
            text: 'Popup text'
            size_hint: .4, .15
            pos_hint:'center_x': .5, 'center_y': .7
            halign: "center"
            valign: "center"

        Button:
            text: "Close"
            size_hint: .4, .15
            pos_hint:'center_x': .5, 'center_y': .15
            on_release: root.close()

【问题讨论】:

【参考方案1】:

您可以通过在open() 方法中保存对Popup 的引用并将close() 方法放在同一个类中来做到这一点。在下面修改后的代码中,Close 按钮现在调用MainWindowclose() 方法:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout


class MainWindow(Screen):
    def open(self):
        pop = Pop()
        self.popup = Popup(title="",
                      content=pop,
                      size_hint=(.8, .8))
        self.popup.open()

    def close(self):
        self.popup.dismiss()


class Pop(FloatLayout):
    pass


class Setting(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_string('''
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

WindowManager:
    MainWindow:
    Setting:


<MainWindow>:
    name: "main"

    FloatLayout:
        Label:
            pos_hint:'center_x': .5, 'center_y': .8
            size_hint:0.5, 0.5
            text: "TITLE"
            font_size: (root.width/30 + root.height/30)

        Button:
            pos_hint:'center_x': .5, 'center_y': .4
            size_hint:0.6, 0.1
            text: "Set"
            on_release:
                app.root.transition = SlideTransition(direction='left')
                app.root.current = "setting"

        Button:
            pos_hint:'center_x': .5, 'center_y': .25
            size_hint:0.6,0.1
            text: "Pop"
            on_release:
                root.open()


<Setting>:
    name: "setting"

    FloatLayout:
        Label:
            text: 'Set Time'
            pos_hint:'center_x': .5, 'center_y': .75
            size_hint: 0.1, 0.1
            font_size: (root.width/30 + root.height/30)

        Button:
            pos_hint:'center_x': .1, 'center_y': .1
            size_hint:0.05,0.05
            on_release:
                app.root.transition = SlideTransition(direction='right')
                app.root.current = 'main'

<Pop>:
    Label:
        text: 'Popup text'
        size_hint: .4, .15
        pos_hint:'center_x': .5, 'center_y': .7
        halign: "center"
        valign: "center"

    Button:
        text: "Close"
        size_hint: .4, .15
        pos_hint:'center_x': .5, 'center_y': .15
        on_release: app.root.get_screen('main').close()
        # if you are sure the current screen will still be "main":
        # on_release: app.root.current_screen.close()
''')


class TesteApp(App):
    def build(self):
        return kv


if __name__ == "__main__":
    TesteApp().run()

【讨论】:

以上是关于在 Kivy 中,如何使用另一个类的关闭按钮关闭弹出窗口?的主要内容,如果未能解决你的问题,请参考以下文章

如何在kivy中引用另一个类方法

自动关闭kivy中的弹出窗口

如何通过 Kivy 与我的计算机通信? [关闭]

Kivy:如何在不关闭弹出窗口的情况下更新弹出标签文本

如何在 Kivy 应用程序启动时满足特定条件时自动弹出警报

如何通过单击栏按钮关闭弹出框