Pyside6加载QMessageBox时报错

ui转换为py文件后,在进行初始化没有问题,但是加载QMessageBox时报错。

from PySide6.QtWidgets import QApplication,QMessageBox,QMainWindow
from ui.patinfo_search import *
import sys


class UI(QMainWindow,Ui_Form):
    def __init__(self,parent = None):
        super(UI,self).__init__(parent)
        # 子类继承了父类的所有属性和方法,父类属性自然会用父类方法来进行初始化
        self.ui = Ui_Form() # 来自 patinfo_search.py 中的类。
        self.ui.setupUi(self)
        self.ui.button1.clicked.connect(self.yahoo)
        self.ui.button2.clicked.connect(self.copy)

    def yahoo(self):
        print('button1 was be knocked')
        QMessageBox.about(self.ui,'反馈结果','hello hello hello')

    def copy(self):
        print('button1 was be knocked')
        info = self.ui.textedit1.toPlainText()
        QMessageBox.about(self.ui,'复制结果',f'{info}')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    uiui = UI()
    uiui.show()
    sys.exit(app.exec_())

py文件

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

################################################################################
## Form generated from reading UI file 'patinfo_search.ui'
##
## Created by: Qt User Interface Compiler version 6.4.3
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPlainTextEdit, QPushButton, QSizePolicy,
    QWidget)

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(737, 556)
        self.button1 = QPushButton(Form)
        self.button1.setObjectName(u"button1")
        self.button1.setGeometry(QRect(490, 150, 81, 31))
        self.button2 = QPushButton(Form)
        self.button2.setObjectName(u"button2")
        self.button2.setGeometry(QRect(490, 270, 81, 31))
        self.textedit1 = QPlainTextEdit(Form)
        self.textedit1.setObjectName(u"textedit1")
        self.textedit1.setGeometry(QRect(120, 90, 291, 281))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"patinfo_search", None))
        self.button1.setText(QCoreApplication.translate("Form", u"\u70b9\u51fb\u6d4b\u8bd5", None))
        self.button2.setText(QCoreApplication.translate("Form", u"\u83b7\u53d6\u4fe1\u606f", None))
    # retranslateUi



点击按钮后提示:

TypeError: 'PySide6.QtWidgets.QMessageBox.about' called with wrong argument types:
  PySide6.QtWidgets.QMessageBox.about(Ui_Form, str, str)
Supported signatures:
  PySide6.QtWidgets.QMessageBox.about(PySide6.QtWidgets.QWidget, str, str)

这可能是因为您在初始化QMessageBox时使用了不正确的参数。请确保在初始化QMessageBox时传递正确的参数,例如:QMessageBox.about(self.ui,'反馈结果','hello hello hello', QMessageBox.Yes | QMessageBox.No)这将使QMessageBox显示“是”或“否”选项,而不是默认的“确定”和“取消”选项

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7737273
  • 你也可以参考下这篇文章:[PyQt5]基本控件6 - 消息框QMessageBox
  • 除此之外, 这篇博客: 四、PyQt对话框的使用中的 1. QMessageBox:对话框 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

          我们平时使用软件时可以看到各种各样的对话框,例如,电脑管家的退出对话框如下:
    在这里插入图片描述

  • 您还可以看一下 章秀淞老师的Python Gui编程-PyQt5开发与实战课程中的 QMessageBox的使用小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    根据您提供的信息,我无法确定您代码中具体的问题。如果您能够提供更详细的错误信息或相关代码段,我可以更好地帮助您解决问题。

    不过,我可以提供一些可能有用的调试步骤:

    1.检查是否已正确安装所有必需的库和模块(例如,PySide6)并导入了正确的类和函数。

    2.检查您的代码是否遵循适当的语法和代码结构,并确认您是否使用了正确的参数和标识符。

    3.确定错误是否出现在加载QMessageBox之前或之后,或是出现在使用特定的QMessageBox函数时。

    4.尝试在加载QMessageBox之前和之后插入和运行其他代码,以检查是否存在与其他部分的交互或冲突。

    以下是一些可能有用的示例代码,可以作为调试和解决问题的起点:

    # 导入必要的库和模块
    import sys
    from PyQt6.QtWidgets import QApplication, QWidget, QMessageBox, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
    
    class App(QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'Qt-加载QMessageBox'
            self.left = 10
            self.top = 10
            self.width = 640
            self.height = 480
            self.initUI()
    
        # 初始化UI
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            # 添加QPushButton
            vbox = QVBoxLayout()
            hbox = QHBoxLayout()
            self.label = QLabel(self)
            self.label.setAlignment(Qt.AlignCenter)
            hbox.addWidget(self.label)
            btn = QPushButton('点击查看消息')
            btn.clicked.connect(self.showDialog)
            vbox.addLayout(hbox)
            vbox.addWidget(btn)
            self.setLayout(vbox)
    
        # 显示QMessageBox
        def showDialog(self):
            mbox = QMessageBox()
            mbox.setIcon(QMessageBox.Information)
            mbox.setWindowTitle('消息对话框')
            mbox.setText('这是一个消息对话框。')
            mbox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
            mbox.buttonClicked.connect(self.msgButtonClick)
            mbox.exec_()
    
        # 消息按钮的状态
        def msgButtonClick(self, i):
            print('Button Clicked is:', i.text())
    
    # 运行App
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        ex.show()
        sys.exit(app.exec_())
    

    希望这些信息对您有所帮助,祝您成功解决问题!


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^