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

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 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示
  • ¥15 AE SDK插件开发,获取关键帧值得问题
  • ¥15 谁知道这个咋搞的吗,有偿
  • ¥20 基于spring boot、的scorm
  • ¥15 往复密封问题的两个问题
  • ¥15 DAC函数和STM32
  • ¥15 任务是接收数据并把数据写入DAC7311,这些代码能实现此功能吗
  • ¥15 分析FP -Growth代码运行内存太大而无法运行的原因
  • ¥20 qtcreat 使用msvc编译器开发软件运行时字体锯齿感严重
  • ¥15 为何显示keyerror fruit