【问题标题】:In Kivy, how do I close a Popup using a close button from another class?在 Kivy 中,如何使用另一个类的关闭按钮关闭弹出窗口?
【发布时间】: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()

【问题讨论】:

    标签: python popup kivy kivy-language


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

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多