【问题标题】:How to close a popup when file is selected from FileChooser从 FileChooser 中选择文件时如何关闭弹出窗口
【发布时间】:2017-03-16 04:58:16
【问题描述】:

当我打开带有FileChooserPopup 时,我可以选择一个文件,但我无法关闭它之后的Popup。有没有人知道如何在从另一个类引用时关闭Popup

class MyFileChooser(FileChooserListView):

    def on_submit(*args):
        fp=args[1][0]

class MainScreen(BoxLayout):

    def filebtn(self, instance):
        self.popup = Popup(title='Select File',
                      content=MyFileChooser(),
                      size_hint=(None, None), size=(400, 400))
        self.popup.open()

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.btnfile = Button(text='Open File')
        self.btnfile.bind(on_press=self.filebtn)
        self.add_widget(self.btnfile)

我试过了

class MyFileChooser(FileChooserListView):
    def on_submit(*args):
        fp=args[1][0]
        popup.dismiss()

但这不起作用,所以我迷路了。任何帮助,将不胜感激。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    好的,我知道了,我将弹出窗口重新定义为全局,然后我可以从 MyFileChooser 类中引用它。

    def filebtn(self, instance):
            global popup
            popup = Popup(title='Select File',
                          content=MyFileChooser(),
                          size_hint=(None, None), size=(400,400))
            popup.open()
    

    然后在我做的 MyFileChooser 类中

    class MyFileChooser(FileChooserListView):
    
        def on_submit(*args):
            print(args[1][0])
            global fp
            fp = args[1][0]
            print(fp)
            popup.dismiss()
    

    【讨论】:

    • 使用全局变量通常被认为是个坏主意。如果您确实需要这样做,通常最好将变量作为 App 实例属性传递,并且可以从 Kivy 应用程序的任何部分访问它。
    • 首先查看这个答案:stackoverflow.com/a/34261423/1542900 您可以在kv 文件中使用app,在Python 代码中使用App.get_running_app() 来获取当前正在运行的App 类实例。
    【解决方案2】:

    Popup 似乎可以作为曾祖父访问:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.uix.button import Button
    from kivy.uix.filechooser import FileChooserListView
    from kivy.lang import Builder
    
    Builder.load_string('''
    <MyWidget>:
        TabbedPanelItem:
            text: 'tab1'
        TabbedPanelItem:
            text: 'tab2'
    ''')
    
    
    class MyFileChooser(FileChooserListView):
        def on_submit(self, *args):
            fp=args[0][0]
            self.parent.parent.parent.dismiss()
    
    
    class MainScreen(BoxLayout):
    
        def filebtn(self, instance):
            self.popup = Popup(title='Select File',
                          content=MyFileChooser(),
                          size_hint=(None, None), size=(400, 400))
            self.popup.open()
    
        def __init__(self, **kwargs):
            super(MainScreen, self).__init__(**kwargs)
            self.orientation = 'vertical'
            self.btnfile = Button(text='Open File')
            self.btnfile.bind(on_press=self.filebtn)
            self.add_widget(self.btnfile)
    
    
    class MyApp(App):
        def build(self):
            return MainScreen()
    
    
    if __name__ == '__main__':
        MyApp().run()
    

    当然,如果您以与弹出内容不同的方式使用 MyFileChooser 类,则此代码会损坏。

    【讨论】:

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