Python 通过导入PyQt5 Ui向按钮添加功能

Python 通过导入PyQt5 Ui向按钮添加功能,python,pyqt,pyqt5,qt-designer,pyuic,Python,Pyqt,Pyqt5,Qt Designer,Pyuic,我有一个名为guiNext.py和next.py的PyQt5用户界面,它正在重新引用该用户界面。如何向UI按钮添加功能?这就是我所拥有的,当我运行next.py并单击HELLO按钮时,不会发生任何事情 guiNext.py: # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'guiNext_001.ui' # # Created by: PyQt5 UI code generator 5.

我有一个名为guiNext.py和next.py的PyQt5用户界面,它正在重新引用该用户界面。如何向UI按钮添加功能?这就是我所拥有的,当我运行next.py并单击HELLO按钮时,不会发生任何事情

guiNext.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'guiNext_001.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_nextGui(object):
    def setupUi(self, nextGui):
        nextGui.setObjectName("nextGui")
        nextGui.resize(201, 111)
        nextGui.setMinimumSize(QtCore.QSize(201, 111))
        nextGui.setMaximumSize(QtCore.QSize(201, 111))
        self.centralwidget = QtWidgets.QWidget(nextGui)
        self.centralwidget.setObjectName("centralwidget")
        self.helloBtn = QtWidgets.QPushButton(self.centralwidget)
        self.helloBtn.setGeometry(QtCore.QRect(10, 10, 181, 91))
        self.helloBtn.setObjectName("helloBtn")
        nextGui.setCentralWidget(self.centralwidget)

        self.retranslateUi(nextGui)
        QtCore.QMetaObject.connectSlotsByName(nextGui)

    def retranslateUi(self, nextGui):
        _translate = QtCore.QCoreApplication.translate
        nextGui.setWindowTitle(_translate("nextGui", "MainWindow"))
        self.helloBtn.setText(_translate("nextGui", "HELLO"))
下面是主文件next.py:

#!usr/bin/env python
#-*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui

class mainProgram(Ui_nextGui):
    def __init__(self, parent=None):
        Ui_nextGui.__init__(self)
        self.setupUi(nextGui)
        self.helloBtn.clicked.connect(self.hello)

    def hello(self):
        print ("HELLO")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    nextGui = QtWidgets.QMainWindow()
    ui = Ui_nextGui()
    ui.setupUi(nextGui)
    nextGui.show()
    sys.exit(app.exec_())

你的程序结构不太正确。有几种方法可以解决这个问题。多重继承方法可能是最直观的。您的代码应该是这样的:

from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui

class mainProgram(QtWidgets.QMainWindow, Ui_nextGui):
    def __init__(self, parent=None):
        super(mainProgram, self).__init__(parent)
        self.setupUi(self)
        self.helloBtn.clicked.connect(self.hello)

    def hello(self):
        print ("HELLO")

if __name__ == "__main__":

    import sys
    app = QtWidgets.QApplication(sys.argv)
    nextGui = mainProgram()
    nextGui.show()
    sys.exit(app.exec_())

super(mainProgram,self)。\uuuuu init\uuuuuu(父级)
必要吗?@Sherafati是-否则
QMainWindow
将无法正确初始化。但是,在Python3中,它可以简化为
super()。\uuuu init\uuuuu(父级)
。但是如果删除该表达式,程序仍然works@Sherafati您必须运行不同的代码。如果在我的示例中删除该行,它将引发一个错误:
RuntimeError:mainProgram类型的super class\uuuu init\uuuu()从未调用过
@Sherafati Yes。如果您在我的回答中按照pyqt文档的链接进行操作,它将显示使用设计器文件的三种不同方式。当您自己创建
QMainWindow
的实例时,它将自动调用自己的
\uuuu init\uuuu
。但是如果在子类中重写
\uuuuu init\uuu
,则必须显式调用基类
\uuuuuu init\uuu