0

I am currently running a PyQt5 application with ipython, but the GUI is frozen after I start some time-consuming commands on the ipython console.

The closest answer that I could find is from How to have Qt run asynchroneously for interactive use like Matplotlib's ion mode? . However, the provided answer there does not solve my problem.

I created a similar example based on the previous post.

from PyQt5.QtWidgets import QApplication, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QMainWindow
import time


%gui qt5

class Rect(QGraphicsRectItem):
  def mousePressEvent(self, event):
    print("foo")

window = QMainWindow()
window.setGeometry(100, 100, 400, 400)
view = QGraphicsView()
scene = QGraphicsScene()
rect = Rect(0, 0, 150, 150)
scene.addItem(rect)
view.setScene(scene)
window.setCentralWidget(view)
window.show()

time.sleep(1000) # Suppose we are running some other commands on ipython console, then the GUI freezes

1 Answer 1

1

It seems that you did not understand what the goal of "%gui qt5" is, that command indicates that ipython will use the Qt eventloop. So if you want to run some time consuming task then you should run it in another thread.

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

4 Comments

I see. In this case, could I put the GUI into a separate thread so that it won't get frozen?
@DauZi No, if you are going to use Qt then the GUI should run on the main thread and the other time consuming ones should run on the child threads to prevent the eventloop from freezing
Is there a way to create a new thread (or QThread), then switch the current ipython console thread to that QThread?
@DauZi No sir, the rules that Qt established are strict: The eventloop must be executed in the main thread.

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.