Search code examples
pythonpython-3.xpyqtpyqt5qmessagebox

python 3.4, setting up QWidgets.QMessageBox


an update to my code, based on the reply of Israel Unterman:

The Error-Class is now

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow

class Error(QtWidgets.QMainWindow):
    reply = False
    last_reply_id = None
    last_id = 0

    def __init__(self, error_code_string, parent=None):
        super().__init__(parent)
        QtWidgets.QMessageBox.warning(self, "Warnung", error_code_string, QtWidgets.QMessageBox.Ok)
        id = give_id(self)

    def give_id(self):
        self.last_id += 1
        return self.last_id

    def give_reply(self):
        if last_id == last_reply_id:
            return self.reply
        else:
            return None

    def set_reply(self, button, id):
        if button in (QMessageBox.Ok, QMessageBox.Yes):
            reply = True
        else:
            reply = False
        self.last_reply_id = id
        return reply

And the Test-Script comes with

from ErrorHandling import Error

Error('Test')

If I am using the normal Code (practically the same Code, just wrapped in a class) the message appears and then at the line

id = give_id(self)

the Code stops without any errormessage from python, just:

Process finished with exit code 1

If I use the test-script, there is nothing (No QMessageBox!) than this:

Process finished with exit code 1

If I debug the Code, init() gets the same Objects and variables, but

super().__init__(parent)

fails without any message. So where is the mistake, or difference.

Here a shortend Version of the Class (it's too long to show all code here), from which the "Error" works nearly fine:

from ErrorHandling import Error
class MainWindow(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        # set some variables
        self.create_layout()

    def create_layout(self):
        # creates a GUI using QWidgets with some Inputboxes and one button

[...]    

    def button_start_clicked(self):
        Error('Check the input')

Here is the old question:

I have a problem regarding the setup of QtWidgets.QMessageBox. All Code follows the description.

The ErrorHandling-Modul should give a message about an error. If needed it may ask a question too. The function ErrorMsg.errorMessage is called from other Moduls in case of an catched exception. There will be more functions added.

If I run the code the following error occurs:

Connected to pydev debugger (build 145.1504)
Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 1531, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevd.py", line 938, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Quellcode/AllgTest.py", line 5, in <module>
    reply = errm.errorMessage('Test')
  File "C:/Quellcode\ErrorHandling.py", line 20, in errorMessage
    msg_box.setIcon(QMessageBox.Warning)
  TypeError: QMessageBox.setIcon(QMessageBox.Icon): first argument of unbound method must have type 'QMessageBox'

Process finished with exit code 1

I tried quite some variations and googled, but I have no idea what the problem is since I found some examples that are using the line QMessageBox.setIcon(QMessageBox.Icon) So where is my mistake?

And now the Code:

There is the following testscript to test my ErrorMsg-class

from ErrorHandling import ErrorMsg
errm = ErrorMsg()
reply = errm.errorMessage('Test')

And here is my ErrorHandling-Modul

from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QMainWindow

class ErrorMsg(QMainWindow):
    def __init__(self):
        pass

    def giveback(self,button):
        if button in (QMessageBox.Ok, QMessageBox.Yes):
            reply = True
        else:
            reply = False
        return reply

    def errorMessage(self, error_msg, buttons='OK'):
        msg_box = QMessageBox
        msg_box.setIcon(QMessageBox.Warning)
        msg_box.setWindowTitle('Warning')
        if buttons == 'OK':
            msg_box.setStandardButtons(QMessageBox.Ok)
        elif buttons == 'YesNo':
            msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        else:
            error_msg = 'Unknown Button >' + buttons + '<, use >OK< or >YesNo<'
            raise ValueError(error_msg)
        msg_box.setText(error_msg)
        clicked_button = msg_box.exec()
        return giveback(clicked_button)

Thanks for your help

James


Solution

  • You didn't create an object of the message box. To create an object use:

    msg_box = QMessageBox()
    

    But you don'y need to go through all this, since QMessageBox has static functions for showing messages, which you can call directly on the QMessageBox class. For example:

    QMessageBox.warning(None, 'title', 'msg')
    

    You also have some control over the butons, see QMessageBox