简体   繁体   English

如何在 PyQt5 中重新打开对话框

[英]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它打印 0,1,2,3,0 然后崩溃错误消息是object QMessageBox is not callable

After calling this line of code self.errordialog(errormessage) , you go into the errordialog function.调用这行代码self.errordialog(errormessage) ,您将进入 errordialog 函数。 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.但是,在errordialog 函数内部,您重新定义了self.errordialog ,使其属于类QMessageBox 而不是类函数,因此当它第二次通过验证时,您尝试调用QMessageBox,这不起作用。

Simply changing the function or variable names will solve this problem.只需更改函数或变量名称即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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