gpt4 book ai didi

python - 当我希望在两个 setText 之间做某事时,pyqt5 QLineEdit.setText 仅在功能中工作一次

转载 作者:行者123 更新时间:2023-12-01 06:45:42 29 4
gpt4 key购买 nike

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize , QTimer
from PyQt5.QtGui import *
from pytube import YouTube
import os
import time
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(600, 200))
self.setWindowTitle("下載影片程式")

self.nameLabel = QLabel(self)
self.nameLabel.setText('網址:')
self.line = QLineEdit(self)

self.line.move(80, 20)
self.line.resize(500, 32)
self.nameLabel.move(20, 20)

pybutton = QPushButton('下載', self)
# pybutton.clicked.connect(self.clickMethod)
pybutton.clicked.connect(self.Download)
# pybutton.clicked.connect(self.Download1)
pybutton.resize(200,32)
pybutton.move(80, 60)

self.statusline = QLineEdit(self)
self.statusline.move(80, 100)
self.statusline.resize(200, 32)

# self.statusline1 = QLineEdit(self)
# self.statusline1.move(80, 140)
# self.statusline1.resize(200, 32)
#
def clickMethod(self):
print('')
def Download(self):
self.statusline.setText('開始下載')
if not os.path.isdir('D:\\下載影片'):
os.mkdir('D:\\下載影片')
yt = YouTube(self.line.text())



video=yt.streams.filter(file_extension='mp4').first()
video.download(r'D:\\下載影片')
QTimer.singleShot(1000, lambda: self.statusline.setText('下載完成'))

# def Download1(self):
# self.statusline.setText('下載完成')


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

我希望它在下载 YouTube 之前显示“开始”,然后显示“结束”。

但它总是只显示“finsh”。

为什么当我点击 QPushButton 时,QLineEdit 只显示“finsh”?

最佳答案

下载文件的任务可能需要很长时间,因此它会阻塞事件循环,导致 GUI 卡住,而卡住的影响是 GUI 无法正确更新。

考虑到上述情况,解决方案是在另一个线程上执行下载。

import functools
import os
import sys

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QSize, QThread, QTimer
from PyQt5.QtWidgets import (
QApplication,
QFormLayout,
QLabel,
QLineEdit,
QMainWindow,
QPushButton,
QSizePolicy,
QWidget,
)

from pytube import YouTube


class DownloadWorker(QObject):
started = pyqtSignal()
finished = pyqtSignal()

@pyqtSlot(str, str)
def download(self, url, path):
self.started.emit()
if not os.path.isdir(path):
os.mkdir(path)
try:
yt = YouTube(url)
video = yt.streams.filter(file_extension="mp4").first()
video.download(path)
except Exception as e:
print(e)

self.finished.emit()


class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)

self.setMinimumSize(QSize(600, 200))
self.setWindowTitle("Download Video Program")

self.line = QLineEdit()
self.pybutton = QPushButton("download")
self.pybutton.setFixedSize(200, 32)
self.statusline = QLineEdit()
self.statusline.setFixedSize(200, 32)

central_widget = QWidget()
self.setCentralWidget(central_widget)

lay = QFormLayout(central_widget)
lay.addRow("URL:", self.line)
lay.addRow("", self.pybutton)
lay.addRow("", self.statusline)

thread = QThread(self)
thread.start()

self.worker = DownloadWorker()
self.worker.started.connect(self.on_started)
self.worker.finished.connect(self.on_finished)
self.worker.moveToThread(thread)

def download(self):
url = self.line.text()
# TODO
path = "/path/of/output.mp4"
wrapper = functools.partial(self.worker.download, url, path)
QTimer.singleShot(0, wrapper)

@pyqtSlot()
def on_started(self):
self.statusline.setText("Started")
self.pybutton.setEnabled(False)

@pyqtSlot()
def on_finished(self):
self.statusline.setText("Finished")
self.pybutton.setEnabled(True)


if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())

关于python - 当我希望在两个 setText 之间做某事时,pyqt5 QLineEdit.setText 仅在功能中工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59232772/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com