How to update PyQt progressbar from an independent function with arguments? - python

I want to use multiple imported function with arguments that takes some while to run. I want a 'working' progress bar that track the processes of that function. I have followed 2 questions already here.
Connect an imported function to Qt5 progress bar without dependencies
Report progress to QProgressBar using variable from an imported module
The difference is that the thread can take any function which can have arguments. The function also not needs to yield the percent to return to the progressbar. The progressbar always start at 0%.
I copied a snippet from first link and modified it for example purpose.
from external_script import long_running_function
class Actions(QDialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Progress Bar')
self.progress = QProgressBar(self)
self.button = QPushButton('Start', self)
self.show()
self.button.clicked.connect(self.onButtonClick)
def onButtonClick(self):
long_running_function(**kwargs) # This can be any function that takes argument/s
self.progress.setValue(value)

Do not get too complicated with the answers as they are limited to a very particular context. In general the logic is to pass a QObject to it that updates the percentage value and then emits a signal with that value. For example a simple solution is to use the threading module:
import sys
import threading
from PyQt5 import QtCore, QtWidgets
class PercentageWorker(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
percentageChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self._percentage = 0
#property
def percentage(self):
return self._percentage
#percentage.setter
def percentage(self, value):
if self._percentage == value:
return
self._percentage = value
self.percentageChanged.emit(self.percentage)
def start(self):
self.started.emit()
def finish(self):
self.finished.emit()
class FakeWorker:
def start(self):
pass
def finish(self):
pass
#property
def percentage(self):
return 0
#percentage.setter
def percentage(self, value):
pass
import time
def long_running_function(foo, baz="1", worker=None):
if worker is None:
worker = FakeWorker()
worker.start()
while worker.percentage < 100:
worker.percentage += 1
print(foo, baz)
time.sleep(1)
worker.finish()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.progress = QtWidgets.QProgressBar()
self.button = QtWidgets.QPushButton("Start")
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.button)
lay.addWidget(self.progress)
self.button.clicked.connect(self.launch)
def launch(self):
worker = PercentageWorker()
worker.percentageChanged.connect(self.progress.setValue)
threading.Thread(
target=long_running_function,
args=("foo",),
kwargs=dict(baz="baz", worker=worker),
daemon=True,
).start()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

Related

PyQT: How to call a function on main thread and get the result (not store the result beforehand)? [duplicate]

I have the following code but it's complaining that I cannot access the UI data from my thread. In my example code below, What is the best way I can access the userInputString value so my threading can run?
self.nameField is a PyQt QLineEdit.
QObject::setParent: Cannot set parent, new parent is in a different thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QWidget::repaint: Recursive repaint detected
import myUI
class MainUIClass(QtGui.QMainWindow, myUI.Ui_MainWindow):
def __init__(self, parent=None):
super(MainUIClass, self).__init__(parent)
self.setupUi(self)
self.startbutton.clicked.connect(self.do_work)
self.workerThread = WorkerThread()
self.connect(self.workerThread, SIGNAL("myThreading()"), self.myThreading, Qt.DirectConnection)
def do_work(self):
self.userInputString = self.nameField.Text()
self.workerThread.start()
def myThreading(self):
if userInputString is not None:
#Do something
class WorkerThread(QThread):
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
def run(self):
self.emit(SIGNAL("myThreading()"))
if __name__ == '__main__':
a = QtGui.QApplication(sys.argv)
app = MainUIClass()
app.show()
a.exec_()
Not sure if it's what you need but here is a working QThread exemple using Qt5
import time
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.worker_thread = WorkerThread()
self.worker_thread.job_done.connect(self.on_job_done)
self.create_ui()
def create_ui(self):
self.button = QtWidgets.QPushButton('Test', self)
self.button.clicked.connect(self.start_thread)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.button)
def start_thread(self):
self.worker_thread.gui_text = self.button.text()
self.worker_thread.start()
def on_job_done(self, generated_str):
print("Generated string : ", generated_str)
self.button.setText(generated_str)
class WorkerThread(QtCore.QThread):
job_done = QtCore.pyqtSignal('QString')
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
self.gui_text = None
def do_work(self):
for i in range(0, 1000):
print(self.gui_text)
self.job_done.emit(self.gui_text + str(i))
time.sleep(0.5)
def run(self):
self.do_work()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = MainWindow()
test.show()
app.exec_()

function print 500 values after 500 iteration function update values to listwidget

how to update listwidget on each iteration ?
it update listwidget after 500 iterations.
pyqt4, python3.7
def input_val(self):
for i in range(500):
time.sleep(1)
self.listWidget.addItem(str(i))
print(i)
You should not use sleep() in the main thread since it blocks the GUI eventloop, if you want to do periodic tasks then you should use a QTImer.
Based on the above the solution is:
from PyQt4 import QtCore, QtGui
class Timer(QtCore.QObject):
timeout = QtCore.pyqtSignal(int)
finished = QtCore.pyqtSignal()
def __init__(self, parent=None, **kwargs):
self._maximum = kwargs.pop("maximum", 0)
_interval = kwargs.pop("interval", 0)
_timeout = kwargs.pop("timeout", None)
_finished = kwargs.pop("finished", None)
if parent is not None:
kwargs["parent"] = parent
super(Timer, self).__init__(**kwargs)
self._counter = 0
self._timer = QtCore.QTimer(timeout=self._on_timeout)
self.interval = _interval
if _timeout:
self.timeout.connect(_timeout)
if _finished:
self.timeout.connect(_finished)
#QtCore.pyqtSlot()
def start(self):
self._timer.start()
#property
def interval(self):
return self._timer.interval()
#interval.setter
def interval(self, v):
self._timer.setInterval(v)
#property
def maximum(self):
return self._maximum
#maximum.setter
def maximum(self, v):
self._maximum = v
#QtCore.pyqtSlot()
def _on_timeout(self):
self.timeout.emit(self._counter)
self._counter += 1
if self._counter >= self.maximum:
self.finished.emit()
self._timer.stop()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.m_listwidget = QtGui.QListWidget()
self.setCentralWidget(self.m_listwidget)
t = Timer(self, maximum=500, interval=1000, timeout=self.onTimeout)
t.start()
#QtCore.pyqtSlot(int)
def onTimeout(self, i):
self.m_listwidget.addItem(str(i))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

PyQt5 Threading

Attempting to write a class that will show the progress of a threaded process. I need to use this class for all "file load" operations; however I am having trouble making it global.
fileloader.py:
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5.QtWidgets import QDialog
from PyQt5.uic import loadUi
class FileLoader(QDialog):
completeSig = pyqtSignal()
def __init__(self, parent=None):
super(FileLoader, self).__init__(parent)
self.filename = ""
self.clientcode = ""
self.thread = ""
loadUi("GlobalUI/fileloader.ui", self)
self.prgLoader.setValue(0)
#pyqtSlot()
def on_btnCancel_clicked(self):
self.close()
def closeEvent(self, e):
self.thread.stop()
def loadData(self):
self.thread.totalSig.connect(self.prgLoader.setMaximum)
self.thread.countSig.connect(self.prgLoader.setValue)
self.thread.finished.connect(self.completed)
self.thread.start()
def completed(self):
self.completeSig.emit()
self.close()
loader.py
from PyQt5.QtCore import pyqtSignal, QThread
from fileloader import FileLoader
class DataLoader(QThread):
totalSig = pyqtSignal(int)
countSig = pyqtSignal(int)
def __init__(self, parent=None):
super(DataLoader, self).__init__(parent)
self.threadactive = True
self.commitsize = 300
self.rowdata = []
def run(self):
print("Code Will Go Here For Loading the File")
def stop(self):
self.threadactive = False
self.wait()
class PatDataLoader():
def load(self, clientcode, filename):
fl = FileLoader()
fl.clientcode = clientcode
fl.filename = filename
fl.thread = DataLoader()
fl.loadData()
I am calling PatDataLoader.load("test","test.txt") from another module. The problem I am running into is the application crashes with QThread: Destroyed while thread is still running as there seems to be a problem with the thread process I am passing to the fileloader. Am I not putting these pieces together properly?
main.py:
from lmdb.patloader import PatDataLoader
class PPSReportsApp(QMainWindow):
def __init__(self, *args):
super(PPSReportsApp, self).__init__(*args)
loadUi("GlobalUI/ppsreportswindow.ui", self)
#self.showMaximized()
#pyqtSlot()
def on_actionTest_triggered(self):
pl = PatDataLoader()
pl.load("TEST","testfile.txt")
In your code pl is a local variable so it will be deleted when it finishes executing on_actionTest_triggered which is an instant possibly generating that problem. On the other hand, no load should be a static method because it does not use the self. self.thread must be None, it is better than ""
How can you prevent pl from being deleted before it is finished processing?
fl is a QDialog so you can use exec_().
fileloader.py
class FileLoader(QDialog):
completeSig = pyqtSignal()
def __init__(self, parent=None):
super(FileLoader, self).__init__(parent)
self.filename = ""
self.clientcode = ""
self.thread = None
loadUi("GlobalUI/fileloader.ui", self)
self.prgLoader.setValue(0)
#pyqtSlot()
def on_btnCancel_clicked(self):
self.close()
def closeEvent(self, e):
if self.thread:
self.thread.stop()
def loadData(self):
if self.thread:
self.thread.totalSig.connect(self.prgLoader.setMaximum)
self.thread.countSig.connect(self.prgLoader.setValue)
self.thread.finished.connect(self.completed)
self.thread.start()
def completed(self):
self.completeSig.emit()
self.close()
loader.py
class DataLoader(QThread):
totalSig = pyqtSignal(int)
countSig = pyqtSignal(int)
def __init__(self, parent=None):
super(DataLoader, self).__init__(parent)
self.threadactive = True
self.commitsize = 300
self.rowdata = []
def run(self):
self.totalSig.emit(1000)
print("Code Will Go Here For Loading the File")
# emulate process
for i in range(1000):
if self.threadactive:
QThread.msleep(10)
self.countSig.emit(i)
def stop(self):
self.threadactive = False
self.quit()
self.wait()
class PatDataLoader():
#staticmethod
def load(clientcode, filename):
fl = FileLoader()
fl.clientcode = clientcode
fl.filename = filename
fl.thread = DataLoader()
fl.loadData()
fl.exec_() # <---
main.py
#pyqtSlot()
def on_actionTest_triggered(self):
PatDataLoader.load("TEST","testfile.txt")

python qt5 thread don't refresh controls in real time

I'm writing an UI using QT5 and python, I added a thread to handle the UI, thread works "fine", a function inside the thread receive 2 strings and return 2 strings (I'm making experiments before develop the real project just to see how it works), as you can see in the code after call the thread function with:
self.requestConexion.emit('lblText1','dddddd')
I call another function that is just a simple counter
self.contador()
so I expect that before the counter finish the value of the control self.lblText1 change, but this is not happen... here is the main code:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import UI_test
import time
import sys
class Threaded(QObject):
result=pyqtSignal(str,str)
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
#pyqtSlot(str,str)
def etiquetas(self,lbl,texto):
print(texto)
self.result.emit(lbl,texto)
class MainApp(QMainWindow, UI_test.Ui_MainWindow):
requestConexion=pyqtSignal(str,str)
def __init__(self, parent=None):
super(MainApp, self).__init__(parent)
self._thread=QThread()
self._threaded=Threaded(result=self.displayLabel)
self.requestConexion.connect(self._threaded.etiquetas)
self._threaded.moveToThread(self._thread)
qApp.aboutToQuit.connect(self._thread.quit)
self._thread.start()
self.setupUi(self)
self.btnStart.clicked.connect(self.conexion)
#pyqtSlot()
def conexion(self):
#self._thread.start()
print(1)
self.requestConexion.emit('lblText1','dddddd')
self.contador()
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:')
if ok:
print(str(text))
#pyqtSlot()
def contador(self):
i=0
while i<50:
print(i)
time.sleep(0.1)
i+=1
#pyqtSlot(str,str)
def displayLabel(self, etiqueta, texto):
self.lblText1.setText(etiqueta)
print(texto)
def main():
app = QApplication(sys.argv)
form = MainApp()
form.show()
app.exec_()
exit(app.exec_())
if __name__ == '__main__':
main()
any idea whats wrong?
I finally find the answer to my question in the next blog:
https://martinfitzpatrick.name/article/multithreading-pyqt-applications-with-qthreadpool/
this is a really great tutorial, after read the documentation in the blog I was able to modify one of the examples to modify the text of a label control in real time; here is the final code:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
class Worker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
self.args = args
self.kwargs = kwargs
self.fn = fn
#pyqtSlot()
def run(self):
#print(self.args, self.kwargs)
print("Thread start")
time.sleep(0.2)
self.fn(*self.args, **self.kwargs) #ejecuta la funcion recibida
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.threadpool = QThreadPool()
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())
self.counter = 0
layout = QVBoxLayout()
self.l = QLabel("Start")
self.l2 = QLabel("xxxxx")
b = QPushButton("DANGER!")
b.pressed.connect(self.oh_no)
layout.addWidget(self.l)
layout.addWidget(self.l2)
layout.addWidget(b)
w = QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
self.show()
self.etiqueta="rrrrrrrrrr"
self.timer = QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
def oh_no(self):
self.etiqueta="hhhhhhhhhhhhhhhhh"
worker = Worker(self.execute_this_fn,'4444')
self.threadpool.start(worker)
def recurring_timer(self):
self.counter +=1
self.l.setText("Counter: %d" % self.counter)
def execute_this_fn(self,x):
print("Hello!")
self.l2.setText(x)
app = QApplication([])
window = MainWindow()
app.exec_()

PyQt update gui

I'm trying to update the text in a Qt GUI object via a QThread in PyQt but I just get the error QPixmap: It is not safe to use pixmaps outside the GUI thread, then it crashes. I would really appreciate any help, thanks.
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.output = Output()
def __del__ (self):
self.ui = None
#pyqtSignature("")
def on_goBtn_released(self):
threadnum = 1
#start threads
for x in xrange(threadnum):
thread = TheThread()
thread.start()
class Output(QWidget, Ui_Output):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setupUi(self)
self.ui = Ui_Output
self.show()
def main(self):
self.textBrowser.append("sdgsdgsgsg dsgdsg dsgds gsdf")
class TheThread(QtCore.QThread):
trigger = pyqtSignal()
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
self.trigger.connect(Output().main())
self.trigger.emit()
self.trigger.connect(Output().main())
This line is problematic. You are instantiating a class in the thread which looks like a widget. This is wrong. You shouldn't use GUI elements in a different thread. All GUI related code should run in the same thread with the event loop.
The above line is also wrong in terms of design. You emit a custom signal from your thread and this is a good way. But the object to process this signal should be the one that owns/creates the thread, namely your MainWindow
You also don't keep a reference to your thread instance. You create it in a method, but it is local. So it'll be garbage collected, you probably would see a warning that it is deleted before it is finished.
Here is a minimal working example:
import sys
from PyQt4 import QtGui, QtCore
import time
import random
class MyThread(QtCore.QThread):
trigger = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def setup(self, thread_no):
self.thread_no = thread_no
def run(self):
time.sleep(random.random()*5) # random sleep to imitate working
self.trigger.emit(self.thread_no)
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.text_area = QtGui.QTextBrowser()
self.thread_button = QtGui.QPushButton('Start threads')
self.thread_button.clicked.connect(self.start_threads)
central_widget = QtGui.QWidget()
central_layout = QtGui.QHBoxLayout()
central_layout.addWidget(self.text_area)
central_layout.addWidget(self.thread_button)
central_widget.setLayout(central_layout)
self.setCentralWidget(central_widget)
def start_threads(self):
self.threads = [] # this will keep a reference to threads
for i in range(10):
thread = MyThread(self) # create a thread
thread.trigger.connect(self.update_text) # connect to it's signal
thread.setup(i) # just setting up a parameter
thread.start() # start the thread
self.threads.append(thread) # keep a reference
def update_text(self, thread_no):
self.text_area.append('thread # %d finished' % thread_no)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwindow = Main()
mainwindow.show()
sys.exit(app.exec_())

Categories

Resources