問題描述
在我從 High Sierra 升級(jí)到 Mojave 之前,我使用 Pyinstaller 創(chuàng)建的應(yīng)用程序運(yùn)行良好.為了演示這個(gè)問題,我創(chuàng)建了一個(gè)簡(jiǎn)單的應(yīng)用程序.主窗口只有一個(gè)按鈕.當(dāng)您按下按鈕時(shí),其文本應(yīng)更改為請(qǐng)稍候"10 秒鐘.當(dāng)我將此程序作為 .py 腳本運(yùn)行時(shí),一切正常,但在使用 Pyinstaller 創(chuàng)建 .app 文件后,它的行為會(huì)有所不同.在您單擊窗口外的任何位置之前,文本不會(huì)更新.
My application, created with Pyinstaller, worked fine until I upgraded from High Sierra to 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. When I run this program as .py script, everything works fine, but after creating .app file with Pyinstaller it behaves differently. The text is not updated until you click anywhere outside of the window.
我嘗試重新安裝 Pyinstaller,但問題仍然存在.
I tried to reinstall Pyinstaller, but the problem still exists.
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
'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_())
推薦答案
我找到了我的問題的答案.為了解決這個(gè)渲染問題,您需要為需要更新的 ui 元素添加以下行.就我而言,僅當(dāng)我需要在 macOS Mojave 上運(yùn)行此應(yīng)用程序時(shí)才需要它.
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. In my case it is required only if I need to run this application on macOS Mojave.
<element>.repaint()
例如:
def click(self):
self.pushButton.setEnabled(False)
self.pushButton.setText("Button is clicked...")
self.pushButton.repaint()
這篇關(guān)于Pyinstaller 和 PyQt5 macOS Mojave 兼容性問題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!