7

I am testing a gui built using PyQt and I would like the ability to interact with the gui using python code that is executed after the PyQt event loop starts (app.exec_()). Another way of saying this is I would like the call to app.exec_ to return immediately as if the gui were modeless, followed by further python code which interacts with the gui.

I found this example of running the PyQt loop in a thread but don't want to do something so unconventional. Is there any way to get the PyQt message loop to continue processing messages while also executing python code in the main thread after exec_ has been called?

3
  • 2
    Can you explain what the problem is with running either the PyQt loop or your own additional code in a second thread? How would you be able to execute two code paths simultaneously without two threads? Commented Feb 4, 2011 at 1:47
  • I can schedule tests in a second thread, but some of the tests involve logic and are more than a single function or object. Is there a way to pass a block of python code to a QThread and have the QThread execute that code as a part of run? Commented Feb 4, 2011 at 4:15
  • It seems I can't use execfile from within a QThread. Commented Feb 4, 2011 at 18:51

5 Answers 5

2

One option here is to use a QtCore.QTimer.singleShot() call to start your python code after calling `exec_()'.

For example:

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    # Setup the GUI.    
    gui = MyGui()
    gui.showMainWindow()

    # Post a call to your python code.
    QtCore.QTimer.singleShot(1000, somePythonFunction)

    sys.exit(app.exec_()) 

This will execute the function somePythonFunction() after 1 second. You can set the time to zero to have the function added immediately queued for execution.

Sign up to request clarification or add additional context in comments.

Comments

1

As a possible easy answer, try not calling app.exec_() in your script and running your PyQt program using python -i My_PyQt_app.py.

For example:

## My_PyQt_app.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)

window = QWidget()
window.show()

# Don't start the event loop as you would do normally!
# app.exec_()

Doing this should allow you to run the GUI through the terminal and interact with it in the command line.

Comments

0

I got it. I can execute the test script line-by-line from the main thread using exec and then run the gui from a worker thread.

Comments

0

Not exactly sure what you wanna do. Are you looking for something like Py(known as PyCrust) for PyQt?

Comments

0

The easiest way is to use IPython:

ipython --gui=qt4

See ipython --help or the online documentation for more options (e.g. gtk, tk, etc).

1 Comment

Note that this relies on the Python inputhook functionality, which runs when the interpreter is idle. If you, for example, type in a long-running loop into the interpreter, the Qt event loop will not run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.