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

时间:2019-08-24 20:04:00

标签: python popup kivy kivy-language

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

我在这里找到了一些解决方案,但是它与我当前使用的ScreenManager不兼容。为了显示我想要的弹出窗口,我使用FloatLayout并将其作为弹出窗口的内容传递。 当我使用该函数关闭时,它在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 个答案:

答案 0 :(得分:1)

您可以通过在Popup方法中保存对open()的引用并将close()方法放在同一类中来实现。在下面的修改后的代码中,Close按钮现在调用close()的{​​{1}}方法:

MainWindow