I am trying "Hello World" using PyQt5 with Python 3.7.3 and macOS 10.14.6. Executing pyqt_helloworld.py below and clicking the button will update the label to "Hello World".
However, when the button is clicked, the text is not changed, and it is not until I focus on the window of other application manually that the label is updated. How can I update QLabel without unfocusing the PyQt application?
Thanks in advance!
pyqt_helloworld_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_HelloWorld(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 40, 201, 21))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 90, 113, 32))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "foobar"))
self.pushButton.setText(_translate("Dialog", "Click"))
pyqt_helloworld.py
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from pyqt_helloworld_ui import Ui_HelloWorld
class HelloWorldGui(QMainWindow, Ui_HelloWorld):
def __init__(self, parent=None):
super(HelloWorldGui, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.setTextHelloWorld)
def setTextHelloWorld(self):
self.label.setText("Hello World")
if __name__ == '__main__':
argvs = sys.argv
app = QApplication(argvs)
hello_world_gui = HelloWorldGui()
hello_world_gui.show()
sys.exit(app.exec_())
pip install PyQt5and it's version is 5.13.0 (latest) - I'm using macOS 10.14.6. - Changing QMainWindow to QDialog in pyqt_helloworld.py does not work. - I-was-a-Ki