0

When I open a Popup with the FileChooser I can select a file, but I cannot close the Popup after it. Does anyone have any idea on how to close the Popup when reference from another class?

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)

I've tried doing

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

But that doesn't work so I'm lost. Any help would be appreciated.

Fogapod
  • 104
  • 1
  • 12

2 Answers2

0

Okay I got It I redefined the popup as a global then I was able to reference it from inside the MyFileChooser Class.

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

Then in the MyFileChooser Class I do

class MyFileChooser(FileChooserListView):

    def on_submit(*args):
        print(args[1][0])
        global fp
        fp = args[1][0]
        print(fp)
        popup.dismiss()
  • Using globals is generaly considered a bad idea. If you really need to do it, it'd be usually better to pass the variable as an `App` instance atribute, and it will be accessible from any part of the Kivy application. – Nykakin Mar 15 '17 at 20:59
  • See this answer for a start: http://stackoverflow.com/a/34261423/1542900 You can use `app` inside `kv` file and `App.get_running_app()` in Python code to obtain currently running instance of `App` class. – Nykakin Mar 15 '17 at 21:22
0

Popup seems to be accessible as the great-grandfather:

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()

Of course this code will broke if you use MyFileChooser class in a different way than a popup content.

Nykakin
  • 8,657
  • 2
  • 29
  • 42