Running threads with PyQt
-
Hi,
I am developing a PyQt application which requires running threads, I would like to know how to run a function in a Worker and at the same time update a progress bar.
The function runs from another python file, i did my import normally. But the function is not recognized when I put it at the run section of my worker
Please find the code below :
class WorkerT(QObject): finished = pyqtSignal() progress = pyqtSignal(int) def run(self): func_train() while True: i = 1 self.progress.emit(i) time.sleep(1) self.finished.emit()
-
Hi and welcome to devnet,
Do you mean "func_train" ?
-
Yes, the function is called "func_train", I imported it from a second python file. I would like to execute it at run section and update a progress bar at the same time to show the progress of the execution of this function.
-
@abdelmoumene said in Running threads with PyQt:
But the function is not recognized
What exactly do you mean by this? Do you get an error message? When it runs the line or when it reads the file & imports? Please paste any error message.
-
I have this error, when I try to debug my application although I have imported the "load.py" file:
(<class 'AttributeError'>, AttributeError ("module 'load' has no attribute 'func_load'"), <traceback object at 0x0000023CA2233B48>)
-
@abdelmoumene
I don't see why the error message statesfunc_load
when you say your code hasfunc_train()
. I don't know whether putting it insiderun()
is relevant. -
I would also like to know if the function "func_train" should be executed inside the while to update the progress bar (the percentage) at the same time or do I have to do this differently?
-
How exactly are you importing that function ?
-
@abdelmoumene
Why do you effectively import twice? Use one or the other? (And preferably notfrom load import *
.) -
Hi,
Can you tell me how to run the function and update the progress bar at the same time?
Thank you.
-
Well, if your function is blocking for a long time then you won't be able to update the progress bar the way you want.
It's up to you to implement it in such a way that it reports progress that can then be used to update the progress bar.
So what does it do ?
-
@SGaist I would like to update the progress bar during the execution of this task so that it shows the execution progress, do you have a solution for me?
-
Not knowing what this task does nor how it is implemented I can only suggest the same thing as my previous answer.
-
Here is an example of QThread and progressBar.
https://stackoverflow.com/questions/68189289/pyqt5-qthread-process-finished-with-exit-code-139-interrupted-by-signal-11-sig/68195024#68195024