【问题标题】:Adding Functions to Buttons via Importing PyQt5 Ui通过导入 PyQt5 Ui 为按钮添加功能
【发布时间】:2017-04-10 14:30:12
【问题描述】:

我有一个名为 guiNext.py 和 next.py 的 PyQt5 Ui,它引用了 UI。如何向 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_())

【问题讨论】:

    标签: python pyqt pyqt5 qt-designer pyuic


    【解决方案1】:

    您的程序结构不太正确。有几种方法可以use the ui files created by Qt Designer。多重继承方法可能是最直观的。你的代码应该是这样的:

    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).__init__(parent) 有必要吗?
    • @Sherafati 是 - 否则 QMainWindow 将无法正确初始化。但是,在 Python 3 中,它可以简化为 super().__init__(parent)
    • 但是我们你删除了那个表达式,程序仍然可以工作
    • @Sherafati 您必须运行不同的代码。如果您在我的示例中删除该行,则会引发错误:RuntimeError: super-class __init__() of type mainProgram was never called
    • @Sherafati 是的。如果您在我的回答中点击指向 pyqt 文档的链接,它会显示三种不同的方式来使用设计器文件。当您自己创建QMainWindow 的实例时,它会自动调用自己的__init__。但是如果你在子类中覆盖__init__,则必须显式调用基类__init__
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多