I want create a simple button to trigger some task. I was using PyQt designer to built GUI and trying to follow the advice that do not edit the UI module directly but put my customized code in a separate module then inherit all GUI aspects.
This is my GUI class:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(507, 424)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(220, 350, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
This is my custom module:
from PyQt5 import QtCore,QtGui,QtWidgets
# main.py is the GUI module
import main
class mainWindow(mainTest.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# I want click this button and print something
self.pushButton.clicked.connect(self.connectionTest)
def connectionTest(self):
print('connected')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = mainTest.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Now my problem is when I run the module and click the pushbutton, it does not trigger the print method. However, if I directly add my custom code in GUI module it will work. I think there's something wrong with the inheritance and I do feel a bit confused about that.So what's the problem here?