Search code examples
pythonpython-3.xbuttonkivypopup

Kivy button fails to close a popup


1

I have made a button that when released opens a popup in kivy, which contains a Float Layout. I want the layout to contain a filechooser, so the user can select a folder to save something, and a button that saves this choice and closes the popup at the same time.

I am currently trying to figure out how to make the button close the popup at first. I came up with this code and I really have no idea why it doesn't close the popup, but it doesn't. I don't get any error at all, just nothing happens. The code is this in kvlang:

#:import Factory kivy.factory.Factory

<BoxLayout3>:
    Label:
        text: "Audio:"
        halign: 'center'
        font_size:'20sp'
    CheckBox:


<BoxLayout4>:
    Label:
        text: "Video:"
        halign: 'center'
        font_size:'20sp'
    CheckBox:

<FileWidget>:
    FileChooserIconView:
        id: filechooser
    Button:
        id:my_button
        text: 'Save'
        size_hint: (0.1, 0.1)
        pos_hint:{'x': 0, 'y': 0}
        on_release:Factory.MyPopup().dismiss()

<MyPopup>:
    id:pop
    auto_dismiss: False
    title: "Select a folder"
    FileWidget:



<FloatLayout1>:
    Button:
        text: 'Folder'
        on_release: Factory.MyPopup().open()
        size_hint: None, None
        size: 130, 50
        pos_hint: {'x':0.2,'y':.4}

<FloatLayout2>:
    Spinner:
        id: spinner_id
        text: "Quality"
        values: ['144p', '240p', '360p', '480p', '720p', '1080p', '1440p', '2160p']
        on_text: root.spinner_clicked(spinner_id.text)
        size_hint: None, None
        size: 130, 50
        pos_hint: {'x': .2, 'y':.4}

<BoxLayout2>:
    cols:4
    BoxLayout3:
    BoxLayout4:
    FloatLayout2:
    FloatLayout1:
    





BoxLayout1:
<BoxLayout1>:
    orientation:'vertical'
    cols: 3
    Label:
        text: "YouTube Downloader"
        halign: 'center'
        bold: True
        font_size:'50sp'
    TextInput:
        size_hint: (.5, .2)
        multiline: False
        hint_text: 'Enter the link of the Youtube video you want to download.'
        pos_hint: {"x": 0.25}
    BoxLayout2:
    Button:
        text: "Submit"
        size_hint: (.5, .2)
        pos_hint: {"x": 0.25}

My python code is(it isn't finished yet):

class MyPopup(Popup):
    pass

class FloatLayout1(FloatLayout):
    pass

class FileWidget(FloatLayout):
    pass

class FloatLayout2(FloatLayout):
    def spinner_clicked(self, value):
        pass

class StackLayout(StackLayout):
    pass

class BoxLayout1(BoxLayout):
    pass

class BoxLayout2(BoxLayout):
    pass

class BoxLayout3(BoxLayout):
    pass

class BoxLayout4(BoxLayout):
    pass

class MyApp(App):
    pass

Solution

  • Your code:

    on_release: Factory.MyPopup().open()
    

    creates a new instance of MyPopup and opens it. Similarly, the code:

    on_release:Factory.MyPopup().dismiss()
    

    also creates a new instance of MyPopup, and then dismisses that instance, which has no effect on the instance of MyPopup that was created by the previous code.

    For this to work correctly, you must call dismiss() on the instance that is open. One way to do this is to keep a reference to the MyPopup instance, so that you can dismiss it later. If you modify your kv to call methods in the App, you can save that needed reference in the App class. So, try changing:

    on_release: Factory.MyPopup().open()
    

    to:

    on_release: app.open_popup()
    

    and change:

    on_release:Factory.MyPopup().dismiss()
    

    to:

    on_release: app.dismiss_popup()
    

    And add those methods to the App:

    class MyApp(App):
    
        def open_popup(self):
            self.popup = MyPopup()
            self.popup.open()
    
        def dismiss_popup(self):
            if self.popup:
                self.popup.dismiss()
                self.popup = None