PyQt5: Using QtDesigner, how do you connect a sign

2019-07-07 17:22发布

问题:

I am a total beginner with PyQt5, please excuse my ignorance
I am trying to connect a push button with a function that I defined

import sys
from PyQt5 import QtGui, QtWidgets,uic


def PrintSomething ():
    print("Hello world")

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = uic.loadUi("Auto.ui")
    window.show()
    sys.exit(app.exec_())

In order to do the above I added a slot to the MainWindow using QtDesigner ..as seen in the picture and then I connected the Pressed signal for the button with the slot of the main window

When I try to run the app I am getting this error:

AttributeError: 'QMainWindow' object has no attribute 'PrintSomething'

What am I doin wrong here ?

Update:

I have also used this code

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Auto import Ui_MainWindow
class MainW (QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
if __name__ == '__main__':
    #topology=topo.LoadTopology()
    app = QApplication(sys.argv)
    myapp = MainW()
    myapp.show()
    sys.exit(app.exec_())

Where the Auto module was generated via pyuic5 and the function PrintSomething() was added. this also results in an error

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(667, 487)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(300, 380, 95, 29))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 667, 23))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuExit = QtWidgets.QMenu(self.menubar)
        self.menuExit.setObjectName("menuExit")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtWidgets.QAction(MainWindow)
        self.actionSave.setObjectName("actionSave")
        self.actionClose = QtWidgets.QAction(MainWindow)
        self.actionClose.setObjectName("actionClose")
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionClose)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuExit.menuAction())

        self.retranslateUi(MainWindow)
        self.pushButton.pressed.connect(MainWindow.PrintSomething())
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuExit.setTitle(_translate("MainWindow", "Exit"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionSave.setText(_translate("MainWindow", "Save"))
        self.actionClose.setText(_translate("MainWindow", "Close"))
    def PrintSomething ():
        print("Hello world")

回答1:

You need to restructure your code like this:

import sys
from PyQt5 import QtGui, QtWidgets, uic

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        uic.loadUi("Auto.ui", self)

    def PrintSomething(self):
        print("Hello world")

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

However, I think it's better to connect up the signals in your code rather than doing it all via Qt Designer:

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        ...
        self.pushButton.clicked.connect(self.PrintSomething)

If you changed the name of the PrintSomething slot, or wanted to connect the button to a different slot, it's quite a lot a hassle to change it via Qt Designer. Signal connections are likely to change quite often while you're developing your application, so coding them manually is probably more manageable.

EDIT:

The other example added to the question can be fixed in a similar way. But if you're going to use pyuic you should never edit the module it generates. Instead, you should simply import it into your main script like this:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Auto import Ui_MainWindow

class MainW (QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.PrintSomething)

    def PrintSomething(self):
        print("Hello world")

if __name__ == '__main__':

    app = QApplication(sys.argv)
    myapp = MainW()
    myapp.show()
    sys.exit(app.exec_())