【发布时间】:2021-08-14 02:08:52
【问题描述】:
我目前正在使用 ipython 运行 PyQt5 应用程序,但是当我在 ipython 控制台上启动一些耗时的命令后,GUI 被冻结。
我能找到的最接近的答案来自 How to have Qt run asynchroneously for interactive use like Matplotlib's ion mode? 。但是,那里提供的答案并不能解决我的问题。
我根据上一篇文章创建了一个类似的示例。
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
【问题讨论】: