When User clicks on "Update" Push Button, box_updatetool_fcn will be executed. MainThread will update the Progressbar in the QMainWindow while the other QThread will download the file. But it executes perfectly, but the problem is signal emited by the WorkerThread is not updating immediately.
Even I have gone through many questions, none solved my problem. I don't know why my fcn_qthread_output is not executing immediately after Qthread finished.
Even the QThread finished() function also executing after the Main Function finished, which I am not using in my current program. I don't know what's wrong in the program, is something missing?
Here is the following console output -
Run Function Closed
Out1 : False, FileName
Main Function Ended
Out2 : True, FileName
What I am expecting is -
Run Function Closed
Out2 : True, FileName
Out1 : True, FileName
Main Function Ended
Below is the program execution flow -
class WorkerThread(QtCore.QThread):
outResult = QtCore.pyqtSignal(tuple)
def __init__(self,target,args,parent=None):
super(WorkerThread, self).__init__(parent)
self.fcn = target
self.args = args
def run(self):
outResult = self.fcn(*self.args)
self.outResult.emit(outResult)
print('Run Function Closed')
class ApplicationWindow(QtWidgets.QMainWindow):
def box_updatetool_fcn(self):
t1 = WorkerThread(target=self.boxapi.fcn_downloadfile, args=(fileID,)) #ThreadWithResult
t1.outResult.connect(self.fcn_qthread_output)
t1.start()
self.box_pbar_fcn(tempfilename,fsize)
print(f'Out1 : {self.var_box_output}')
self.boxapi.fcn_updatetool(file_fullpath,self.progpath)
print('Main Function Ended')
def fcn_qthread_output(self,result):
self.var_box_output = result
print(f'Out2 : {self.var_box_output}')
def box_pbar_fcn(self,filename,fullsize):
pobj = self.ui.progressbar
while(barprogress < 100):
if os.path.exists(filename):
currfilesize = os.path.getsize(filename)
barprogress = int(currfilesize/fullsize*100)
pobj.setValue(barprogress)
Reproducible Code -
I have used box_pbar_fcn (progress bar which shows the downloading progress) and t1 Thread (downloading the file) will Run in simultaneously.
import os, sys
from PyQt5 import QtWidgets, QtGui, QtCore
class WorkerThread(QtCore.QThread):
outResult = QtCore.pyqtSignal(tuple)
def __init__(self,parent=None):
super(WorkerThread, self).__init__(parent)
def run(self):
outResult = (True,'Out1')
self.outResult.emit(outResult)
print('Run Function Closed')
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.var_box_output = (False,'Inp1')
self.box_updatetool_fcn()
def box_updatetool_fcn(self):
tempfilename,fsize = 'Test',300
t1 = WorkerThread() #ThreadWithResult
t1.outResult.connect(self.fcn_qthread_output)
t1.start()
self.box_pbar_fcn(tempfilename,fsize)
print(f'Out1 : {self.var_box_output}')
print('Main Function Ended')
def fcn_qthread_output(self,result):
self.var_box_output = result
print(f'Out2 : {self.var_box_output}')
def box_pbar_fcn(self,filename,fullsize):
pass
#pobj = self.ui.progressbar
#while(barprogress < 100):
#if os.path.exists(filename):
# currfilesize = os.path.getsize(filename)
# barprogress = int(currfilesize/fullsize*100)
# pobj.setValue(barprogress)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mw = ApplicationWindow()
mw.show()
sys.exit(app.exec_())
The problem is that you are blocking the main thread with your while True (There should never be an instruction that blocks the eventloop for long). In Qt the logic is to do the job asynchronously. In this case I suspect that in a thread you are downloading a file and you want to show the download progress in the QProgressBar and for this you are continuously measuring the size of the file. If so, a possible solution is to use a QTimer to run the periodic task of measuring the file size.
Disclaimer: The following code that has not been tested should work. As I do not know how is the way you save the file, it could be that it does not work since many functions only write the file at the end and not in parts for efficiency reasons.
import os
import sys
from functools import cached_property
from PyQt5 import QtCore, QtGui, QtWidgets
class Downloader(QtCore.QThread):
outResult = QtCore.pyqtSignal(tuple)
def run(self):
import time
time.sleep(10)
outResult = (True, "Out1")
self.outResult.emit(outResult)
print("Run Function Closed")
class FileObserver(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._filename = ""
self._current_size = 0
#property
def filename(self):
return self._filename
def start(self, filename, interval=100):
self._current_size = 0
self._filename = filename
self.timer.setInterval(interval)
self.timer.start()
def stop(self):
self.timer.stop()
#cached_property
def timer(self):
timer = QtCore.QTimer()
timer.timeout.connect(self._measure)
return timer
def _measure(self):
if os.path.exists(self.filename):
file_size = os.path.getsize(filename)
if self._current_size != file_size:
self._current_size = file_size
self.file_size_changed.emit(file_size)
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.var_box_output = (False, "Inp1")
self.box_updatetool_fcn()
#cached_property
def file_observer(self):
return FileObserver()
def box_updatetool_fcn(self):
tempfilename, fullsize = "Test", 300
self.ui.progressbar.setMaximum(fullsize)
t1 = WorkerThread(self)
t1.outResult.connect(self.fcn_qthread_output)
t1.finished.connect(self.file_observer.stop)
t1.start()
self.file_observer.start(tempfilename, interval=10)
def fcn_qthread_output(self, result):
self.var_box_output = result
print(f"Out2 : {self.var_box_output}")
def box_pbar_fcn(self, file_size):
self.ui.progressbar.setValue(file_size)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mw = ApplicationWindow()
mw.show()
sys.exit(app.exec_())
Related
I am trying to load some data which takes 30+ seconds. During this time I wish the user to see a small GUI which says "Loading .", then "Loading ..", then "Loading ...", then "Loading ." etc. I have done some reading and I think I have to put this in a separate thread. I found someone who had a similar problem suggesting the solution was this in the right spot:
t = threading.Thread(target=self.test)
t.daemon = True
t.start()
In a lower part of the file I have the test function
def test(self):
tmp = InfoMessage()
while True:
print(1)
and the InfoMessage function
from PyQt5 import uic, QtCore, QtGui, QtWidgets
import sys
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
try:
super(InfoMessage, self).__init__(parent)
uic.loadUi('ui files/InfoMessage.ui',self)
self.setWindowTitle(' ')
self.o_msg = msg
self.msg = msg
self.info_label.setText(msg)
self.val = 0
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
self.show()
except BaseException as e:
print(str(e))
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
QtWidgets.QApplication.processEvents()
def main():
app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
form = InfoMessage('Loading ') # We set the form to be our MainWindow (design)
app.exec_() # and execute the app
if __name__ == '__main__': # if we're running file directly and not importing it
main() # run the main function
When I run the InfoMessage function alone it works fine and it updates every 0.5 seconds etc. However, when I fun this as part of the loading file the GUI is blank and incorrectly displayed. I know it is staying in the test function because of the print statement in there.
Can someone point me in the right direction? I think I am missing a couple of steps.
First, there are two ways of doing this. One way is to use the Python builtin threading module. The other way is to use the QThread library which is much more integrated with PyQT. Normally, I would recommend using QThread to do threading in PyQt. But QThread is only needed when there is any interaction with PyQt.
Second, I've removed processEvents() from InfoMessage because it does not serve any purpose in your particular case.
Finally, setting your thread as daemon implies your thread will never stop. This is not the case for most functions.
import sys
import threading
import time
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtCore import QThread
def long_task(limit=None, callback=None):
"""
Any long running task that does not interact with the GUI.
For instance, external libraries, opening files etc..
"""
for i in range(limit):
time.sleep(1)
print(i)
if callback is not None:
callback.loading_stop()
class LongRunning(QThread):
"""
This class is not required if you're using the builtin
version of threading.
"""
def __init__(self, limit):
super().__init__()
self.limit = limit
def run(self):
"""This overrides a default run function."""
long_task(self.limit)
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
super(InfoMessage, self).__init__(parent)
uic.loadUi('loading.ui', self)
# Initialize Values
self.o_msg = msg
self.msg = msg
self.val = 0
self.info_label.setText(msg)
self.show()
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
def loading_stop(self):
self.timer.stop()
self.info_label.setText("Done")
class MainDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
# QThread Version - Safe to use
self.my_thread = LongRunning(limit=10)
self.my_thread.start()
self.my_loader = InfoMessage('Loading ')
self.my_thread.finished.connect(self.my_loader.loading_stop)
# Builtin Threading - Blocking - Do not use
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10}
# )
# self.my_thread.start()
# self.my_loader = InfoMessage('Loading ')
# self.my_thread.join() # Code blocks here
# self.my_loader.loading_stop()
# Builtin Threading - Callback - Use with caution
# self.my_loader = InfoMessage('Loading ')
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10,
# 'callback': self.my_loader}
# )
# self.my_thread.start()
def main():
app = QtWidgets.QApplication(sys.argv)
dialog = MainDialog()
app.exec_()
if __name__ == '__main__':
main()
Feel free to ask any follow up questions regarding this code.
Good Luck.
Edit:
Updated to show how to run code on thread completion. Notice the new parameter added to long_task function.
I have GUI application which uses qwebview to make web automation process through long loop so I used QThread to do this but I can't terminate the thread, my code is below
class Main(QMainWindow):
def btStart(self):
self.mythread = BrowserThread()
self.connect(self.mythread, SIGNAL('loop()'), self.campaign_loop, Qt.AutoConnection)
self.mythread.start()
def btStop(self):
self.mythread.terminate()
def campaign_loop(self):
loop goes here
class BrowserThread(QThread):
def __init__(self):
QThread.__init__(self)
def run(self):
self.emit(SIGNAL('loop()'))
this code is working fine in starting thread but fail to stop the loop and browser still running even if I call close event to it and it disappears from GUI
EDIT: it works on linux too, I tried this on raspberry pi 4 and it works fine
the point is to make the main loop in the " run " method because " terminate " function is stopping the loop in " run " not the thread its self
here is a working example for this, but unfortunately it works on windows only
import sys
import time
from PySide.QtGui import *
from PySide.QtCore import *
class frmMain(QDialog):
def __init__(self):
QDialog.__init__(self)
self.btStart = QPushButton('Start')
self.btStop = QPushButton('Stop')
self.counter = QSpinBox()
self.layout = QVBoxLayout()
self.layout.addWidget(self.btStart)
self.layout.addWidget(self.btStop)
self.layout.addWidget(self.counter)
self.setLayout(self.layout)
self.btStart.clicked.connect(self.start_thread)
self.btStop.clicked.connect(self.stop_thread)
def stop_thread(self):
self.th.stop()
def loopfunction(self, x):
self.counter.setValue(x)
def start_thread(self):
self.th = thread(2)
#self.connect(self.th, SIGNAL('loop()'), lambda x=2: self.loopfunction(x), Qt.AutoConnection)
self.th.loop.connect(self.loopfunction)
self.th.setTerminationEnabled(True)
self.th.start()
class thread(QThread):
loop = Signal(object)
def __init__(self, x):
QThread.__init__(self)
self.x = x
def run(self):
for i in range(100):
self.x = i
self.loop.emit(self.x)
time.sleep(0.5)
def stop(self):
self.terminate()
app = QApplication(sys.argv)
win = frmMain()
win.show()
sys.exit(app.exec_())
I have the following two files:
import sys
import time
from PyQt4 import QtGui, QtCore
import btnModule
class WindowClass(QtGui.QWidget):
def __init__(self):
super(WindowClass, self).__init__()
self.dataLoaded = None
# Widgets
# Buttons
thread = WorkerForLoop(self.runLoop)
# thread.start()
self.playBtn = btnModule.playpauselBtnClass \
('Play', thread.start)
# Layout
layout = QtGui.QHBoxLayout()
layout.addWidget(self.playBtn)
self.setLayout(layout)
# Window Geometry
self.setGeometry(100, 100, 100, 100)
def waitToContinue(self):
print self.playBtn.text()
while (self.playBtn.text() != 'Pause'):
pass
def runLoop(self):
for ii in range(100):
self.waitToContinue()
print 'num so far: ', ii
time.sleep(0.5)
class WorkerForLoop(QtCore.QThread):
def __init__(self, function, *args, **kwargs):
super(WorkerForLoop, self).__init__()
self.function = function
self.args = args
self.kwargs = kwargs
def __del__(self):
self.wait()
def run(self):
print 'let"s run it'
self.function(*self.args, **self.kwargs)
return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wmain = WindowClass()
wmain.show()
sys.exit(app.exec_())
and the second file btnModule.py:
from PyQt4 import QtGui, QtCore
class playpauselBtnClass(QtGui.QPushButton):
btnSgn = QtCore.pyqtSignal()
def __init__(self, btnName, onClickFunction):
super(playpauselBtnClass, self).__init__(btnName)
self.clicked.connect(self.btnPressed)
self.btnSgn.connect(onClickFunction)
def btnPressed(self):
if self.text() == 'Play':
self.setText('Pause')
self.btnSgn.emit()
print 'Changed to pause and emited signal'
elif self.text() == 'Pause':
self.setText('Continue')
print 'Changed to Continue'
elif self.text() == 'Continue':
self.setText('Pause')
print 'Changed to Pause'
If in the first file I remove the comment at thread.start() it works as expected, it starts the thread and then hangs until I click Play on the UI. However, I thought it should work even if I didn't start it there as the signal is btnSgn is connected to onClickFunction which in this case takes the value thread.start.
Used this as a reference
http://joplaete.wordpress.com/2010/07/21/threading-with-pyqt4/
I think the reason it doesn't work when you try to call thread.start() in your second file (via self.btnSgn.emit()) is that the thread object goes out of scope outside of the init function where you created it. So you are calling start() on an already deleted thread.
Just changing thread -> self.thread (i.e. making the thread object a member of the WindowClass object) works fine when I tried it, since thread is then kept alive till the end of the program.
I have i python application, which uses PyQt GUI. It application has some I/O operations, which i want to run in separate threads.
When each thread started, it should write messages to applications main window status bar and when last thread is completed, status bar messages should be cleared.
How can i handle number of threads via QThread?
Here is example of code:
import sys, time
from PyQt4 import QtCore, QtGui
from functools import partial
def io_emulator(sleep_seconds):
print 'We will sleep for %s seconds' % str(sleep_seconds)
time.sleep(sleep_seconds)
class IOThread(QtCore.QThread):
def __init__(self, func):
QtCore.QThread.__init__(self)
self.io_func = func
def run(self):
self.io_func()
class m_Window(QtGui.QWidget):
def __init__(self):
super(m_Window, self).__init__()
self.initUI()
def initUI(self):
self.thread_button = QtGui.QPushButton("Thread", self)
self.thread_button.move(30, 10)
self.spinbox = QtGui.QSpinBox(self)
self.spinbox.move(30, 50)
self.stat_label = QtGui.QLabel("", self)
self.stat_label.setGeometry(QtCore.QRect(200, 200, 150, 14))
self.stat_label.move(30,90)
self.setWindowTitle('Threads')
self.show()
self.thread_button.clicked.connect(self._sleeper)
def _sleeper(self):
seconds = int(self.spinbox.text())
stat_str = 'Sleeping %s seconds' % str(seconds)
io_func = partial(io_emulator, seconds)
set_status_f = partial(self.set_status_msg, stat_str)
self.thread = IOThread(io_func)
self.thread.started.connect(set_status_f)
self.thread.finished.connect(self.clear_status_msg)
self.thread.start()
def set_status_msg(self, msg):
self.stat_label.setText(msg)
def clear_status_msg(self):
self.stat_label.clear()
def main():
app = QtGui.QApplication(sys.argv)
m = m_Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I want, that message is being cleared only when last thread is ended.
You cannot call QWidget functions from any thread but the main thread. If you want another thread to trigger GUI operations, then they should be communicated by emitting signals that are connected to your main thread:
def someFunc():
return "message"
class IOThread(QtCore.QThread):
statusUpdate = QtCore.pyqtSignal(str)
def __init__(self, func):
QtCore.QThread.__init__(self)
self.io_func = func
def run(self):
msg = self.io_func()
self.statusUpdate.emit(msg)
Then in your main thread, connect the io thread to the status label or some intermediate handler:
io_thread = IOThread(someFunc)
io_thread.statusUpdate.connect(self.status_label.setText)
This will create a queued connection, which places the call into the event loop of the main thread for execution.
I'm trying to learn how to use threading in a python program. I'm using PySide and QThreads since I'm going to implement gui afterwards with PySide.
I have understood the main consept of threading, at least I think. But I'm still confused with event loops. And I think that is the problem with my aplication.
Here is a sample application that I can't get to work properly.
In my main class I have several worker threads and I want to them to report their progress to the main main class. But the main program don't print progress messages in real time.
How could I get this to work?
from PySide import QtCore
import time, sys
class MyWorkerThread(QtCore.QThread):
message = QtCore.Signal(str)
def __init__(self, id, parent=None):
super(MyWorkerThread, self).__init__(parent)
self.id = id
def run(self):
for i in range(10):
self.message.emit("%d: %d" % (self.id, i))
time.sleep(0.2)
class MainProgram():
def __init__(self, parent=None):
self.threads = []
self.addWorker(MyWorkerThread(1))
self.addWorker(MyWorkerThread(2))
def addWorker(self, worker):
worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
self.threads.append(worker)
def startWorkers(self):
for worker in self.threads:
worker.start()
worker.wait()
self.workersFinished()
def workersFinished(self):
QtCore.QCoreApplication.instance().quit()
#QtCore.Slot(str)
def printMessage(self, text):
sys.stdout.write(text+'\n')
sys.stdout.flush()
if __name__ == '__main__':
app = QtCore.QCoreApplication(sys.argv)
m = MainProgram()
m.startWorkers()
sys.exit(app.exec_())
worker.wait() is the problem. This call blocks the main thread (in this case the one running event loop) until the worker finishes its job.
Here is a slightly changed version (I've commented my changes):
from PySide import QtCore
import time, sys
class MyWorkerThread(QtCore.QThread):
message = QtCore.Signal(str)
def __init__(self, id, parent=None):
super(MyWorkerThread, self).__init__(parent)
self.id = id
def run(self):
for i in range(10):
self.message.emit("%d: %d" % (self.id, i))
time.sleep(0.2)
class MainProgram():
def __init__(self, parent=None):
self.threads = []
self.addWorker(MyWorkerThread(1))
self.addWorker(MyWorkerThread(2))
def addWorker(self, worker):
worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
# connect the finished signal to method so that we are notified
worker.finished.connect(self.workersFinished)
self.threads.append(worker)
def startWorkers(self):
for worker in self.threads:
worker.start()
# no wait, no finished. you start the threads and leave.
def workersFinished(self):
if all(worker.isFinished() for worker in self.threads):
# wait until all the threads finished
QtCore.QCoreApplication.instance().quit()
#QtCore.Slot(str)
def printMessage(self, text):
sys.stdout.write(text+'\n')
sys.stdout.flush()
if __name__ == '__main__':
app = QtCore.QCoreApplication(sys.argv)
m = MainProgram()
m.startWorkers()
sys.exit(app.exec_())