Fosiul
Fosiul

Reputation: 17

How to pass QWidget parameter into a class

I am learning PyQt5, I am trying to create a message box , but its keep giving me error

Traceback (most recent call last):
  File "addition_code_main.py", line 47, in clickMethod
    QMessageBox.about(self, "Title", "Message")
TypeError: about(QWidget, str, str): argument 1 has unexpected type 'MainWindow_EXEC'

The code I have is is bellow, I am not understanding what i am doing wrong, where to pass this Qwidget parameter,

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QMessageBox

from addition_code import Ui_MainWindow


class MainWindow_EXEC():

    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)

        MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(MainWindow)
        self.connect_singals()

        MainWindow.show()

        sys.exit(app.exec_())

    def connect_singals(self):
        self.ui.pushButton_3.clicked.connect(self.clickMethod)

    def clickMethod(self):
        QMessageBox.about(self, "Title", "Message")


if __name__ == "__main__":
    MainWindow_EXEC()

addition_code.py

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

# Form implementation generated from reading ui file 'addition_code.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(150, 40, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(430, 40, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(290, 40, 113, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(260, 100, 81, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(400, 110, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(250, 190, 75, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Addtion"))
        self.pushButton_2.setText(_translate("MainWindow", "MysqlConnect"))
        self.pushButton_3.setText(_translate("MainWindow", "MessagesBox"))

Upvotes: 1

Views: 1112

Answers (1)

eyllanesc
eyllanesc

Reputation: 243973

To understand the problem, just check the documentation:

void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)

Displays a simple about box with title title and text text. The about box's parent is parent.

about() looks for a suitable icon in four locations:

  1. It prefers parent->icon() if that exists.

  2. If not, it tries the top-level widget containing parent.

  3. If that fails, it tries the active window.

  4. As a last resort it uses the Information icon.

The about box has a single button labelled "OK". On macOS, the about box is popped up as a modeless window; on other platforms, it is currently application modal.

As you can see the first parameter must be a QWidget or None but you are passing it a MainWindow_EXEC instance that does not meet that condition by throwing that error.

Considering the above there are several possible solutions:

  • Pass None as the first parameter.

    def clickMethod(self):
        QMessageBox.about(None, "Title", "Message")
  • Move to MainWindow so you have to become a class member.

class MainWindow_EXEC:
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)

        self.MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.MainWindow)
        self.connect_singals()

        self.MainWindow.show()

        sys.exit(app.exec_())

    def connect_singals(self):
        self.ui.pushButton_3.clicked.connect(self.clickMethod)

    def clickMethod(self):
        QMessageBox.about(self.MainWindow, "Title", "Message")

Upvotes: 1

Related Questions