如何修复 QtWidgets.QMessageBox 中的“TypeError ......”以在 PyQT5 中弹出消息

Posted

技术标签:

【中文标题】如何修复 QtWidgets.QMessageBox 中的“TypeError ......”以在 PyQT5 中弹出消息【英文标题】:How to fix "TypeError................" in QtWidgets.QMessageBox for popup message in PyQT5 【发布时间】:2019-01-30 18:49:16 【问题描述】:

当用户在 PyQT5-GUI 应用程序中提交空白或不正确的信息作为用户名和密码时,我想向用户显示弹出信息/警告/等消息。

我很困惑,我不知道问题的根本原因在哪里。

Windows 10 - 64 位 ___ Python 3.6 和 3.7 - 64 位 ____ PyQT5 - 5.11.3

class Ui_MainWindow(object):
def __init__(self):
  <skipped>
  self.btn_signin.clicked.connect(self.check_userPass) #### User/Pass checking.
  <skipped>

def check_userPass(self):  # an internal method/function from `Ui_MainWindow`.
  username = self.txt_user.text()  #a QlineEdit
  password = self.txt_pass.text()  #a QlineEdit
  if len(username) < 2  and  len(password) < 2:
    QtWidgets.QMessageBox.question(self, "Checkk", "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel)
    ###error for above line:
    ##Traceback (most recent call last):
    ##  File ".\demo_codes.py", line 215, in check_userPass
    ##    QtWidgets.QMessageBox.question(self, 'List manipulation', "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
    ##TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'

    QtWidgets.QMessageBox.question("Checkk", "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Cancel)
    ###error for above line:
    ##Traceback (most recent call last):
    ##  File ".\demo_codes.py", line 215, in check_userPass
    ##    QtWidgets.QMessageBox.question('List manipulation', "Helllooo", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
    ##TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'str'

if __name__ == "__main__":
  import sys
  app = QtWidgets.QApplication(sys.argv)
  MainWindow = QtWidgets.QMainWindow()
  ui = Ui_MainWindow()
  #ui.setupUi(MainWindow) # i renamed `setupUi` to __init__.
  MainWindow.show()
  sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

试试看:

from PyQt5 import QtWidgets, QtCore, QtGui

class Ui_MainWindow(object):
#    def __init__(self):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 350)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.txt_user = QtWidgets.QLineEdit(self.centralwidget)
        self.txt_user.setGeometry(QtCore.QRect(320, 130, 113, 20))
        self.txt_pass = QtWidgets.QLineEdit(self.centralwidget)
        self.txt_pass.setGeometry(QtCore.QRect(320, 170, 113, 20))

        self.btn_signin = QtWidgets.QPushButton("btn_signin\n`Fusion` style", self.centralwidget)
        self.btn_signin.setGeometry(QtCore.QRect(320, 200, 113, 50))        
        MainWindow.setCentralWidget(self.centralwidget)    

        # <skipped>
        self.btn_signin.clicked.connect(self.check_userPass) 
        # <skipped>

    def check_userPass(self):  
        username = self.txt_user.text()  #a QlineEdit
        password = self.txt_pass.text()  #a QlineEdit
        if len(username) < 2  and  len(password) < 2:
            """
#            QtWidgets.QMessageBox.question(
            QtWidgets.QMessageBox.information(
                 MainWindow,      # - self , + MainWindow  !!!
                "Checkk", 
                "Helllooo `Ali reza`", 
#                QtWidgets.QMessageBox.Ok, 
#                QtWidgets.QMessageBox.Cancel
                )
            """
# +++
            msg = QtWidgets.QMessageBox()
            msg.setIcon(QtWidgets.QMessageBox.Information)

            msg.setText("لطفا نام کاربری و رمزعبور خود را با دقت وارد نمایید")
            msg.setInformativeText("This is additional information")
            msg.setWindowTitle("MessageBox demo")
            msg.setDetailedText("The details are as follows:")
            msg.setStandardButtons(QtWidgets.QMessageBox.Ok ) #| QtWidgets.QMessageBox.Cancel)
            msg.buttonClicked.connect(self.msgbtn)

            retval = msg.exec_()                    # <<<---- !!!

            print("value of pressed message box button:", retval)
# +++
    def msgbtn(self, i):
        print( "Button pressed is:",i.text() )             


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('Fusion')            # <<<--- convert app to FUSION style in win10

    MainWindow = QtWidgets.QMainWindow()

    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)   

    MainWindow.show()
    sys.exit(app.exec_())

【讨论】:

@s-nick,你知道我如何在 win10 中将我的应用程序转换为 FUSION 风格吗!? @s-nick,非常感谢。问题出在“self”和“MainWindow”上!???是的 !??。亲爱的尼克,我如何设置流行音乐的图标和字体样式和字体大小!!? tutorialspoint.com/pyqt/pyqt_qmessagebox.htm popup = QtWidgets.QMessageBox() popup.setIcon(QtWidgets.QMessageBox.Information) popup.setText("لطفا نام کاربری و رمزعبور خود را با دقت وارد نمایید.") popup.是附加信息") popup.setWindowTitle("MessageBox demo") popup.setDetailedText("详情如下:") popup.setStandardButtons(QtWidgets.QMessageBox.Ok) ############# #setIcon 不起作用。并且没有弹出消息....!!! ***.com/questions/45386146/… #FUSION 风格适用于 PyQT 5.11.3 ***.com/questions/4008649/… #多项目选择。

以上是关于如何修复 QtWidgets.QMessageBox 中的“TypeError ......”以在 PyQT5 中弹出消息的主要内容,如果未能解决你的问题,请参考以下文章

如何修复漏洞

如何修复WMI

PHP网站漏洞怎么修复 如何修补网站程序代码漏洞

如何修复这些漏洞? (npm audit fix 无法修复这些漏洞)

如何修复AppScan漏洞

如何在DOS环境下修复系统