Bo Milanovich
Bo Milanovich

Reputation: 8203

Make the worker thread wait for user input in GUI thread? Python/PyQt

I have a multi-threaded application written in Python in which one thread "takes care" of the GUI, and the other is the worker thread. At one point however, the worker thread, in the midst of processing data, emits a signal with a QString which connects to the display_image() function in the GUI thread. The display_image() function requires a user to input a line of text.

My question is, how can I make the worker thread wait with data processing until the display_image() function returns a value, that is, until the user presses the OK button?

GUI.py

class GUI(QMainWindow):
  def __init__(self, parent=None):
    super, etc
    self.worker = worker.Worker()

  def display_image(self, image):         
     """wait for user input"""

Worker.py

class Worker(QThread):
  def __init__(self, parent=None):
    super, etc

  def run(self):
     self.emit(SIGNAL("imageFound(QString)"), image)
     #wait until...
     self.inputted_user_text = inputted_user_text # < this is what I need to figure out

Upvotes: 2

Views: 1506

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226181

The easiest way to communicate between threads is with the Queue module. Have the GUI thread put an "okay" value and have the worker do a blocking get to receive the okay signal.

Upvotes: 2

Related Questions