問題描述
我在執(zhí)行以下 gui 時遇到問題.如果沒有消息框,它可以正常工作,但是當有消息框時,它會阻塞.知道為什么有消息時gui會阻塞.謝謝
I have a problem while executing the following gui. it works normally if there's no msgbox, but when there is a mesbox it blocks. any idea why the gui blocks when there is message. thank you
from PyQt5 import QtCore, QtGui, QtWidgets
import threading
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.pressed.connect(self.threadingc)
def calculation(self):
for i in range(10):
time.sleep(1)
print(i)
msg = QtWidgets.QMessageBox()
msg.setInformativeText('Finish')
msg.exec_()
def threadingc(self):
x=threading.Thread(target=self.calculation)
x.start()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
推薦答案
任何訪問 UI 元素只允許在主 Qt 線程內.請注意,access 不僅意味著讀取/寫入小部件屬性,還意味著創(chuàng)建;任何從其他線程執(zhí)行此操作的嘗試在最好的情況下都會導致圖形問題或不一致的行為,而在最壞(和更常見)的情況下會導致崩潰.
Any access to UI elements is only allowed from within the main Qt thread. Note that access means not only reading/writing widget properties, but also creation; any attempt to do so from other threads results in graphical issues or incosistent behavior in the best case, and a crash in the worst (and more common) case.
這樣做的唯一正確方法是使用帶有(可能)自定義信號的 QThread:這允許 Qt 正確地對信號進行排隊,并在它可以實際處理它們時對它們做出反應.
The only correct way to do so is to use a QThread with (possibly) custom signals: this allows Qt to correctly queue signals and react to them when it can actually process them.
以下是一個非常簡單的情況,不需要創(chuàng)建 QThread 子類,但請考慮這只是為了教育目的.
The following is a very simple situation that doesn't require creating a QThread subclass, but consider that this is just for educational purposes.
class Ui_MainWindow(object):
# ...
def calculation(self):
for i in range(10):
time.sleep(1)
print(i)
def showMessage(self):
msg = QtWidgets.QMessageBox()
msg.setInformativeText('Finish')
msg.exec_()
self.pushButton.setEnabled(True)
def threadingc(self):
self.pushButton.setEnabled(False)
self.thread = QtCore.QThread()
# override the `run` function with ours; this ensures that the function
# will be executed in the new thread
self.thread.run = self.calculation
self.thread.finished.connect(self.showMessage)
self.thread.start()
請考慮以下重要方面:
- 我必須禁用按鈕,否則可以在前一個線程仍在執(zhí)行時創(chuàng)建一個新線程;這會產生問題,因為覆蓋
self.thread
會導致 python 在運行時嘗試垃圾收集(刪除)前一個線程,這是一個非常糟糕的 東西; - 一個可能的解決方案是使用父線程創(chuàng)建線程,這通常使用簡單的
QThread(self)
來完成,但在您的情況下這是不可能的,因為 Qt 對象只能接受其他Qt 對象作為它們的父對象,而在您的情況下self
將是一個Ui_MainWindow
實例(這是一個基本的 python 對象); - 以上幾點是一個重要問題,因為您嘗試從
pyuic
生成的文件開始執(zhí)行程序,而這永遠不會這樣做:這些文件是打算保持原樣而無需任何手動修改,并且僅用作導入的模塊;閱讀有關 使用 Designer 的官方指南中有關此主題的更多信息;另請注意,試圖模仿這些文件的行為是沒有用的,因為這通常會導致對象結構非常混亂; - 理論上,您可以添加對 qt 對象的引用(例如,通過在
setupUi()
函數中添加self.mainWindow = MainWindow
)并使用該引用 (thread = QThread(self.mainWindow)
),或將線程添加到持久列表 (self.threads = []
,再次在setupUi()
),但由于上述原因,我強烈建議您不要這樣做;
- I had to disable the pushbutton, otherwise it would be possible to create a new thread while the previous one still executing; this will create a problem, since overwriting
self.thread
will cause python to try to garbage collect (delete) the previous thread while running, which is a very bad thing; - a possible solution to this is to create the thread with a parent, which is usually done with a simple
QThread(self)
, but that's not possible in your case because Qt objects can accept only other Qt objects as their parent, while in your caseself
would be aUi_MainWindow
instance (which is a basic python object); - the above point is an important issue, because you're trying to implement your program starting from a
pyuic
generated file, which should never be done: those files are intended to be left as they are without any manual modification, and used only as imported modules; read more about this topic on the official guidelines about using Designer; also note that trying to mimic the behavior of those files is useless, as normally leads to great confusion about object structure; - you could theoretically add a reference to a qt object (for example, by adding
self.mainWindow = MainWindow
in thesetupUi()
function) and create the thread with that reference (thread = QThread(self.mainWindow)
), or add the thread to a persistent list (self.threads = []
, again in thesetupUi()
), but due to the above point I strongly discourage you to do so;
最后,更正確代碼實現將要求您再次生成 ui 文件,保持原樣并執(zhí)行類似以下示例的操作;請注意,我添加了一個非常基本的異常實現,它還展示了如何正確地與自定義信號交互.
Finally, a more correct implementation of your code would require you to generate again the ui file, leave it as it is and do something like the following example; note that I added a very basic exception implementation that also shows how to correctly interact with custom signals.
from PyQt5 import QtCore, QtGui, QtWidgets
from mainwindow import Ui_MainWindow
import time
class Calculation(QtCore.QThread):
error = QtCore.pyqtSignal(object)
def run(self):
for i in range(10):
time.sleep(1)
print(i)
try:
10 / 0
except Exception as e:
self.error.emit(e)
break
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.pressed.connect(self.threadingc)
def showMessage(self):
msg = QtWidgets.QMessageBox()
msg.setInformativeText('Finish')
msg.exec_()
def threadingc(self):
# create the thread with the main window as a parent, this is possible
# since QMainWindow also inherits from QObject, and this also ensures
# that python will not delete it if you want to start another thread
thread = Calculation(self)
thread.finished.connect(self.showMessage)
thread.error.connect(self.showError)
thread.start()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
在上述情況下,使用以下命令處理 ui 文件(顯然,假設 ui 名為mainwindow.ui"):
In the above case, the ui file was processed using the following command (assuming that the ui is named "mainwindow.ui", obviously):
pyuic mainwindow.ui -o mainwindow.py
這篇關于線程時出現 msgbox 錯誤,GUI 塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!