简体   繁体   中英

How to reopen a dialog box in PyQt5

I'm trying to use a dialog box in order to report an error to the user. It seems to be working if there is one error although when there are multiple it doesn't reopen when closed with the next error and instead crashes.

def errordialog(self, errormessage):
    self.errordialog = QMessageBox()
    self.errordialog.addButton("OK", 0)
    self.errordialog.setText(errormessage)
    self.errordialog.exec()

And this is the main program

def validate_data(data, regex):
    if re.match(regex, data, re.VERBOSE):
        error = False
    else:
        errormessage = "ERROR"
        print("0")
        self.errordialog(errormessage)
        print("1")
        self.errordialog.accept() # I added this in while trying to solve the issue
        print("2")
        error = True
    return error

data = supplierid
regex = "[A-Z]$"
error = validate_data(data, regex)
print("3")
data = suppliername
regex = ".(1,50)$"
error = validate_data(data, regex)
print("4")

It prints 0,1,2,3,0 and then crashes the error message is object QMessageBox is not callable

After calling this line of code self.errordialog(errormessage) , you go into the errordialog function. However, inside the errordialog function, you have redefined self.errordialog so that it is of class QMessageBox instead of class function, so when it goes through the validation the second time, you attempt to call the QMessageBox, which does not work.

Simply changing the function or variable names will solve this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM