Home > Software engineering >  Popup in Kivy Python does not get dismissed for more than one time
Popup in Kivy Python does not get dismissed for more than one time

Time:07-21

I have made a popup to show up when an error occurs, with a button that closes the popup when clicked. All worked well until I tried getting an error for 2 times. In the second time the popup did not close and I am wondering if there is a way to work it out.

This is the python code:


class MyPopup(Popup):
    filechooser= ObjectProperty(None)

class ErrorPopup(Popup):
    filechooser= ObjectProperty(None)


class Main(FloatLayout):
    audio_check= ObjectProperty(None)
    video_check= ObjectProperty(None)
    spinner_id= ObjectProperty(None)
    yt_link=ObjectProperty(None)
    name=ObjectProperty(None)
    error_label=ObjectProperty(None)
    def submit_text(self):
        self.error_popup=MyApp()
        if self.audio_check.active and self.video_check.active:
            try:
                YouTube(self.yt_link.text).streams.filter(res=self.spinner_id.text).first().download(path, filename=self.name.text '.mp4')
            except:
                self.error_popup.error()
        elif self.audio_check.active and not self.video_check.active:
            try:
                YouTube(self.yt_link.text).streams.filter(only_audio=True).first().download(path, filename=self.name.text '.mp3')
            except:
                self.error_popup.error()
        elif not self.audio_check.active and self.video_check.active:
            try:
                YouTube(self.yt_link.text).streams.filter(res=self.spinner_id.text, only_video =True).first().download(path, filename=self.name.text '.mp4')
            except:
                self.error_popup.error()
        elif not self.audio_check.active and not self.video_check.active:
            self.error_popup.error()
    def spinner_clicked(self, value):
        self.ids.spinner_id.text= value
    


class MyApp(App):
    def build(self):
        return Main()
    def open_popup(self):
        self.popup = MyPopup()
        self.popup.open()

    def dismiss_popup(self):
        self.popup.dismiss()
        global path
        path=self.popup.filechooser.path
    
    def error(self):
        self.error_popup= ErrorPopup()
        self.error_popup.open()
    def try_again(self):
        self.error_popup.dismiss()
        print("Andrew")


            


if __name__ == '__main__':
    MyApp().run()

This is the kivy code:

<MyPopup>:
    auto_dismiss: False
    title: "Select a folder"
    filechooser: filechooser
    FloatLayout:
        FileChooserIconView:
            id: filechooser
        Button:
            id:my_button
            text: 'Save'
            size_hint: (0.1, 0.1)
            pos_hint:{'x': 0, 'y': 0}
            on_release: app.dismiss_popup()

<ErrorPopup>:
    title: "Error"
    Button:
        text: "Try Again"
        on_release: app.try_again()





<Main>:
    audio_check: audio_check
    video_check: video_check
    spinner_id: spinner_id
    yt_link: yt_link
    name: name
    folder_button: folder_button
    BoxLayout:
        orientation:'vertical'
        cols: 4
        Label:
            text: "YouTube Downloader"
            halign: 'center'
            bold: True
            font_size:'50sp'
        TextInput:
            id: yt_link
            size_hint: (.5, .2)
            multiline: False
            hint_text: 'Enter the link of the Youtube video you want to download.'
            pos_hint: {"x": 0.25}
        TextInput:
            id: name
            size_hint: (.5, .2)
            multiline: False
            hint_text: 'Enter the name you want the file to have.'
            pos_hint: {"x": 0.25}
        BoxLayout:
            cols:4
            BoxLayout:
                Label:
                    text: "Audio:"
                    halign: 'center'
                    font_size:'20sp'
                CheckBox:
                    id: audio_check
            BoxLayout:
                Label:
                    text: "Video:"
                    halign: 'center'
                    font_size:'20sp'
                CheckBox:
                    id: video_check
            FloatLayout:
                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}
            FloatLayout:
                Button:
                    id:folder_button
                    text: 'Folder'
                    on_release: app.open_popup()
                    size_hint: None, None
                    size: 130, 50
                    pos_hint: {'x':0.2,'y':.4}
        Button:
            text: "Submit"
            size_hint: (.5, .2)
            pos_hint: {"x": 0.25}
            on_release: root.submit_text()

CodePudding user response:

The problem is in your submit() method. The code:

self.error_popup=MyApp()

is creating a new instance of MyApp, then the code:

self.error_popup.error()

is calling the error() method from that new instance of MyApp. You need to be calling the methods of the App that you are running. To do that, just remove the line:

self.error_popup=MyApp()

And replace:

self.error_popup.error()

with:

App.get_running_app().error()

This makes sure that you are calling the error() method of the App that you are running.

  • Related