У меня ошибка вроде этого:
QObject::startTimer: Timers can only be used with threads started with QThread
Мой весь код:
from PyQt5.QtWidgets import *
from newCode import mp3Download
from PyQt5.QtCore import QTimer,QThread
from threading import Thread
import sys
class ListDownload(QWidget):
def __init__(self,path):
super().__init__()
self.path = path
self.time = 100
self.initUI()
def initUI(self):
self.VerticalLayout = QVBoxLayout()
self.SeasonOfWitch = QLabel()
self.SeasonOfWitch.setText("Enter the URLs : ")
self.URLs = QTextEdit()
self.URLs.setText("""enter the addresses in the here .a row for each url.
Delete this message before entering the URLs.""")
self.horizontalControlLayout = QHBoxLayout()
self.download = QPushButton("Download")
self.download.clicked.connect(self.videoDownload)
self.Cancel = QPushButton("Cancel")
self.Cancel.clicked.connect(self.cancelFunc)
self.horizontalControlLayout.addWidget(self.download)
self.horizontalControlLayout.addWidget(self.Cancel)
self.VerticalLayout.addWidget(self.SeasonOfWitch)
self.VerticalLayout.addWidget(self.URLs)
self.VerticalLayout.addLayout(self.horizontalControlLayout)
self.setLayout(self.VerticalLayout)
def cancelFunc(self):
self.close()
def videoDownload(self):
self.urlList = self.URLs.toPlainText().split("\n")
row = 1
errorList = list()
for url in self.urlList:
if 'www.youtube.com' in url.split("/") and (url.startswith("https://") or url.startswith("http://")):
row+=1
else:
errorList.append(row)
row+=1
decrease = 0#Each element deleting index changes the lenght of the list.Cause of that.
for row in errorList:
self.urlList.pop(row-1-decrease)
decrease += 1
messageObj = Thread(target=self.messageAnimation,name="message")
downloadObj = Thread(target=self.downloadFunc,name="download")
messageObj.start()
downloadObj.start()
while not(downloadObj.is_alive()):
messageObj._stop()
def downloadFunc(self):
mp3Download(self.urlList,self.path)
def messageAnimation(self):
def timerFunc():
self.animatedMessageFunc("Downloading ....")
self.timer = QTimer()
self.timer.timeout.connect(timerFunc)
self.timer.start(1000)
def remove_widget(self,layout,widget_name):
layout.removeWidget(widget_name)
widget_name.deleteLater()
widget_name = None
def animatedMessageFunc(self,message):
animatedMessage = message
self.URLs.clear()
iterator = iter(range(len(animatedMessage)))
for i in range(len(animatedMessage)):
QTimer.singleShot(self.time,lambda :self.URLs.setText(self.URLs.toPlainText()+animatedMessage[next(iterator)]))
self.time += 50
self.time = 100
Проблема в таймерах.В animatedMessageFunc()
.Я хочу запустить две функции одновременно. Я использую Thread
класс из threading
модуля.Я хочу этого, потому что, когда self.downloadFunc()
заканчивается, мне нужно остановить функцию self.messageAnimation()
.Я пытаюсь использовать QThread вместо Thread.Но я не могу понять, как мне использовать этот класс. Есть ошибки как мои. Но это ошибки в Java или другом языке. Я не могу найти свое решение в этих вопросах.