python - Kivy Python 中的弹出窗口不会被解雇超过一次

标签 python python-3.x kivy popup kivy-language

我制作了一个弹出窗口,在发生错误时显示,并带有一个按钮,在单击时关闭弹出窗口。一切正常,直到我尝试出错 2 次。第二次弹出窗口没有关闭,我想知道是否有办法解决这个问题。

这是python代码:


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

这是kivy代码:

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

最佳答案

问题出在您的 submit() 方法中。代码:

self.error_popup=MyApp()

正在创建一个新的 MyApp 实例,然后是代码:

self.error_popup.error()

正在从 MyApp 的新实例调用 error() 方法。您需要调用正在运行的 App 的方法。为此,只需删除以下行:

self.error_popup=MyApp()

并替换:

self.error_popup.error()

与:

App.get_running_app().error()

这确保您正在调用正在运行的 Apperror() 方法。

关于python - Kivy Python 中的弹出窗口不会被解雇超过一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73059107/

相关文章:

python - kivy如何旋转图片

python - Django - 过滤早于 X 天的对象

python - 将本地 dist 包安装到 virtualenv 中

python - 函数在 session 中有效但不是 Keras 损失函数

python - 获取描述符对象的好方法

python - 如果我取随机整数,如何除无余数?

python - 调整CSV数据: appending cells to previous row,合并包含特定字符串的单元格

python - 在 Kivy 中创建全局变量

python - pytest 无法导入通过 pip 安装且包含下划线的模块

android - 排行榜中的分数未更新