2

Currently i am working on a GUI program in PyQt5. My program has several steps which run after each other. After finish each step i want to print for example Step1 finished and then Step2 finished and so on. Since the main window freezes the print does not work. It would be very helpful if you give me some solutions.

Orca
  • 475
  • 1
  • 12

1 Answers1

1

A simple solution would be a thread based solution, where you set an event flag after finishing the task:

import threading


done_flag = threading.Event()

def print_status():
    while True:
        done_flag.wait()
        print("I'm done!")
        done_flag.clear()

Inside your function, you have to call .set_flag. This functions blocks until your function have finished (if you always call set_flag).

It's by the way always good practice to use threads in GUI applications. This avoids freezing cause the main loop etc. cannot run.

By the way, instead of raw printing, I would suggest using logging which gives more useful information.

bilaljo
  • 358
  • 1
  • 6
  • 13
  • 1
    Thank you very much for your reply. Since i am not much familiar with threads, where should i put .set_flag ? for example i have a function to sum up two numbers. In which part should i put .set_flag and when should i use print_status() function? – Orca Oct 05 '22 at 12:31
  • Can you please help me how can i put my main function in a thread and other parts in other thread? – Orca Oct 05 '22 at 12:34
  • Take a look at this question: https://stackoverflow.com/questions/2905965/creating-threads-in-python Your main function shouldn't be a in a own thread. Otherwise pyqt will freez (can't find the source right now). You could spawn your threads before you call the main loop or after an event (like pressing a button). set_flag can be called after each function has finished it's calculation, to signal that print can be called. – bilaljo Oct 05 '22 at 13:39
  • Thank you very much for your complete answer. It was helpful for me – Orca Oct 06 '22 at 06:58