更新我的代码的基础上,以色列Unterman的回复:现在蟒蛇3.4,设置QWidgets.QMessageBox
误差类是
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
和测试脚本带有
from ErrorHandling import Error
Error('Test')
如果我使用的正常码(实际上是相同的代码,只是包裹在一个类)出现的消息,然后在该行
id = give_id(self)
代码停止,不从蟒蛇任何errormessage的,只是:
Process finished with exit code 1
如果我使用的测试脚本,没有什么(无QMessageBox提示!)不是这样的:
Process finished with exit code 1
如果我调试代码,INIT()获取相同的对象和变量,但
super().__init__(parent)
失败,没有任何消息。 那么错误或差异在哪里。
这里类的shortend版本(这是太长,在这里展示的所有代码),从“错误”的作品几乎罚款:
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')
这里是老问题:
我QtWidgets.QMessageBox的设置有问题。 所有代码都遵循说明。
ErrorHandling-Modul应该给出关于错误的消息。 如果需要,也可以提出一个问题。 在捕捉异常的情况下,函数ErrorMsg.errorMessage是从其他Moduls调用的。 会增加更多功能。
如果我运行代码出现以下错误:
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
我尝试颇有些变化和GOOGLE了,但我不知道是什么问题,因为我发现,正在使用线路 QMessageBox提示一些例子.setIcon(QMessageBox.Icon) 那么我的错误在哪里?
而现在的代码:
有以下testscript来测试我的ERRORMSG级
from ErrorHandling import ErrorMsg
errm = ErrorMsg()
reply = errm.errorMessage('Test')
这里是我的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)
感谢您的帮助
James
正确的,但他还需要使用'调用'giveback'方法时self';) –
@Israel Unterman 我需要的功能,因为在未来的发展中,有必要知道,什么按钮被点击,所以我需要一个返回值。 –