[英]Pyinstaller and PyQt5 macOS Mojave compatibility issues
My application, created with Pyinstaller, worked fine until I upgraded from High Sierra to Mojave. 我用Pyinstaller创建的应用程序工作正常,直到我从High Sierra升级到Mojave。 In order to demonstrate the issue I create the simple application. 为了演示这个问题,我创建了一个简单的应用程序。 Main window has only one button. 主窗口只有一个按钮。 When you press the button its text should be changed to "Please wait" for 10 seconds. 当您按下按钮时,其文本应更改为“请等待”10秒钟。 When I run this program as .py script, everything works fine, but after creating .app file with Pyinstaller it behaves differently. 当我以.py脚本运行此程序时,一切正常,但在使用Pyinstaller创建.app文件后,它的行为有所不同。 The text is not updated until you click anywhere outside of the window. 在单击窗口外的任何位置之前,文本不会更新。
I tried to reinstall Pyinstaller, but the problem still exists. 我试图重新安装Pyinstaller,但问题仍然存在。
from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(303, 304)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 80, 300, 43))
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(80, 170, 113, 32))
self.pushButton.setObjectName("pushButton")
self.pushButton.setDefault(True)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.click)
self.thread = Thread()
self.thread.finished.connect(lambda: self.pushButton.setEnabled(True))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Click me"))
def click(self):
if not self.thread.isRunning():
self.pushButton.setEnabled(False)
self.pushButton.setText("Please wait")
self.label.setText("The button below should display \n 'Please wait' for 10 seconds")
self.thread.start()
class Thread(QtCore.QThread):
def run(self):
time.sleep(10)
ui.pushButton.setEnabled(False)
ui.pushButton.setText("Click me")
ui.label.setText("")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I found an answer to my question. 我找到了一个问题的答案。 In order to solve this rendering issue you need to add the following line for a ui element, which needs to be updated. 为了解决此渲染问题,您需要为ui元素添加以下行,该行需要更新。 In my case it is required only if I need to run this application on macOS Mojave. 在我的情况下,只有当我需要在macOS Mojave上运行此应用程序时才需要它。
<element>.repaint()
For example: 例如:
def click(self):
self.pushButton.setEnabled(False)
self.pushButton.setText("Button is clicked...")
self.pushButton.repaint()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.