laomu0515 2022-09-03 19:01 采纳率: 100%
浏览 141
已结题

Python Qt报错 'QMainWindow' object has no attribute 'on_pushButton'

QT designer 上设计了界面,并关联 button click 响应槽函数

UIC 生成的py文件

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

# Form implementation generated from reading ui file 'jav.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(759, 367)
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(12)
        MainWindow.setFont(font)
        MainWindow.setMouseTracking(False)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(190, 0, 551, 341))
        self.textBrowser.setObjectName("textBrowser")
        self.formLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.formLayoutWidget.setGeometry(QtCore.QRect(10, 90, 176, 62))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.pagenum = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.pagenum.setObjectName("pagenum")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.pagenum)
        self.label = QtWidgets.QLabel(self.formLayoutWidget)
        self.label.setObjectName("label")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label)
        self.comboBox = QtWidgets.QComboBox(self.formLayoutWidget)
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.setItemText(10, "1~100")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.comboBox)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.NoMask = QtWidgets.QRadioButton(self.centralwidget)
        self.NoMask.setGeometry(QtCore.QRect(60, 30, 89, 16))
        self.NoMask.setObjectName("NoMask")
        self.Mask = QtWidgets.QRadioButton(self.centralwidget)
        self.Mask.setGeometry(QtCore.QRect(60, 60, 89, 16))
        self.Mask.setObjectName("Mask")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(MainWindow.on_pushButton)   #click 关联on_pushButton 自定义函数处理
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_2.setText(_translate("MainWindow", "爬取指定页号:"))
        self.label.setText(_translate("MainWindow", "爬取页号范围:"))
        self.comboBox.setItemText(0, _translate("MainWindow", "1~5"))
        self.comboBox.setItemText(1, _translate("MainWindow", "1~10"))
        self.comboBox.setItemText(2, _translate("MainWindow", "1~20"))
        self.comboBox.setItemText(3, _translate("MainWindow", "1~30"))
        self.comboBox.setItemText(4, _translate("MainWindow", "1~40"))
        self.comboBox.setItemText(5, _translate("MainWindow", "1~50"))
        self.comboBox.setItemText(6, _translate("MainWindow", "1~60"))
        self.comboBox.setItemText(7, _translate("MainWindow", "1~70"))
        self.comboBox.setItemText(8, _translate("MainWindow", "1~80"))
        self.comboBox.setItemText(9, _translate("MainWindow", "1~90"))
        self.pushButton.setText(_translate("MainWindow", "确认"))

主函数文件


```python
# -*- coding: utf-8 -*-
import sys
from PyQt5.Qt import *
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,QMainWindow
from jav import Ui_MainWindow
from PyQt5.QtCore import Qt,pyqtSlot,QObject,pyqtSlot,pyqtSignal
from PyQt5 import uic

class MyMainClass(QtWidgets.QMainWindow,Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow
        self.ui.setupUi(self)

    def on_pushButton(self,event):
        print("111111111")


if __name__ == '__main__':
    # 创建一个应用程序对象
    app = QtWidgets.QApplication(sys.argv)
    # 创建一个空白控件(窗口)
    ui = uic.loadUi("E:\Python\qt\jav.ui")
    # 设置窗口标题
    ui.setWindowTitle("Jav")
    # 移动窗口位置
    ui.move(300, 400)
    # 显示窗口
    ui.show()
    # 进入程序的主循环,并通过exit函数确保主循环安全结束
    sys.exit(app.exec_())







最后报错 
AttributeError: 'QMainWindow' object has no attribute 'on_pushButton'  


MainWindow没有这个 'on_pushButton' , 这个函数自定义的一个名字,请问哪里有问题?
  • 写回答

2条回答 默认 最新

  • laomu0515 2022-09-03 20:13
    关注

    自己定义的 MyMainClass 有问题, main函数没实例化自己的类对象,初学qt 有点生疏。

    
    
    ```python
    # -*- coding: utf-8 -*-
    import sys
    from PyQt5.Qt import *
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,QMainWindow
    from jav import Ui_MainWindow
    from PyQt5.QtCore import Qt,pyqtSlot,QObject,pyqtSlot,pyqtSignal
    from PyQt5 import uic
    
    class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow): # 多重继承QMainWindow和Ui_MainWindow
    
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
    
    
        def on_pushButton(self,event):
            QMessageBox.information(self, '提示信息', '按钮1被按下。')
    
    
    if __name__ == '__main__':
        # 创建一个应用程序对象
        app = QtWidgets.QApplication(sys.argv)
        # 创建一个空白控件(窗口)
        ui = MainWindow()
        # 设置窗口标题
        ui.setWindowTitle("Jav")
        # 移动窗口位置
        ui.move(300, 400)
        # 显示窗口
        ui.show()
        # 进入程序的主循环,并通过exit函数确保主循环安全结束
        sys.exit(app.exec_())
    
    
    
    

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 9月3日

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题