亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在函數中使用線程來防止 gui 凍結?

如何在函數中使用線程來防止 gui 凍結?

森欄 2022-10-11 17:09:53
我正在從視頻制作一個簡單的音頻下載器,但是每當我單擊下載按鈕時,gui 都會停止,直到下載完成。我以為我可以使用線程來處理這些事情,但幾乎有數百種方法可以實現這一點,但我不知道我應該選擇哪一種,這就是為什么我很困惑然后想問你的原因。我的代碼在這里:import sysimport threadingimport pafyfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButtonfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import pyqtSlotclass App(QWidget):    def __init__(self):        super().__init__()        self.title = 'PyQt5 button - pythonspot.com'        self.left = 100        self.top = 100        self.width = 320        self.height = 200        self.initUI()    def initUI(self):        self.setWindowTitle(self.title)        self.setGeometry(self.left, self.top, self.width, self.height)        button = QPushButton('Coffee shop radio', self)        button.move(10,10)        button.clicked.connect(self.on_click)        self.show()    def on_click(self):        url = "https://www.youtube.com/watch?v=IcvruhYk0po"        video = pafy.new(url)        bestaudio = video.getbestaudio()        bestaudio.download()if __name__ == '__main__':    app = QApplication([])    ex = App()    sys.exit(app.exec_())稍微修改了代碼,它可以工作了,謝謝大家。import sysimport threadingimport pafyfrom time import sleepfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEditfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import pyqtSlotclass App(QWidget):    threads = []    def __init__(self):        super().__init__()        self.title = 'YouDio'        self.left = 100        self.top = 100        self.width = 280        self.height = 90        self.initUI()    def initUI(self):        self.setWindowTitle(self.title)        self.setGeometry(self.left, self.top, self.width, self.height)        button = QPushButton('DOWNLOAD', self)        button.move(10,25)        button.clicked.connect(self.on_click)        self.line = QLineEdit(self)        self.line.move(120,27)        self.show()    def on_click(self):        self.t = threading.Thread(target=self.threaded)        self.t.start()
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

from threading import Thread

import sys

import threading

import pafy

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

from PyQt5.QtGui import QIcon

from PyQt5.QtCore import pyqtSlot



def download_me():

    url = "https://www.youtube.com/watch?v=IcvruhYk0po"

    video = pafy.new(url)


    bestaudio = video.getbestaudio()

    bestaudio.download()  


class App(QWidget):


    def __init__(self):

        super().__init__()

        self.title = 'PyQt5 button - pythonspot.com'

        self.left = 100

        self.top = 100

        self.width = 320

        self.height = 200

        self.initUI()


    def initUI(self):

        self.setWindowTitle(self.title)

        self.setGeometry(self.left, self.top, self.width, self.height)


        button = QPushButton('Coffee shop radio', self)

        button.move(10,10)

        button.clicked.connect(self.on_click)


        self.show()


    def on_click(self):

        t = Thread(target=download_me)

        t.daemon = True

        t.start()




if __name__ == '__main__':

    app = QApplication([])

    ex = App()

    sys.exit(app.exec_())

嘗試這個!


實際上下載任務是同步完成的,所以你的ardard會阻塞直到下載任務結束……你必須把這部分代碼放在一個守護線程中。


注意:我不知道 python 線程是否可以與 Qt 混合,所以你應該使用好的 Lib 但想法保持不變


查看完整回答
反對 回復 2022-10-11
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號