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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

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

      悬赏问题

      • ¥15 和您碰到了同一个问题
      • ¥15 如何在微信支付V3PHP版请求中添加header?
      • ¥15 在数字信号处理中用MATLAB显示波形。
      • ¥15 keil MDK软件进行仿真调试的局限
      • ¥15 关于gateway产生临时文件夹的问题
      • ¥15 Java Broken Pipe以及OOM
      • ¥20 请问有没有RTL8211FS的参考原理图
      • ¥50 esp32的platfromio工程添加文件就会编译失败
      • ¥15 linux下运行VASP报错
      • ¥15 python里面查找赋值,如何可以在剩余未空值的行里面进行进一步的筛选呢?