【发布时间】:2016-08-12 09:08:54
【问题描述】:
根据 Israel Unterman 的回复更新我的代码:
Error-Class 现在是
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)
代码在没有来自 python 的任何错误消息的情况下停止,只是:
Process finished with exit code 1
如果我使用测试脚本,没有什么比这个(没有 QMessageBox!):
Process finished with exit code 1
如果我调试代码,init() 会得到相同的对象和变量,但是
super().__init__(parent)
失败,没有任何消息。 那么错误或差异在哪里。
这里是类的缩短版本(这里显示所有代码太长了),其中“错误”几乎可以正常工作:
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。 会增加更多功能。
如果我运行代码会出现以下错误:
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
我尝试了很多变体并用谷歌搜索,但我不知道问题出在哪里,因为我发现了一些使用该行的示例 QMessageBox.setIcon(QMessageBox.Icon) 那么我的错误在哪里?
现在是代码:
有以下测试脚本来测试我的ErrorMsg-class
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)
感谢您的帮助
詹姆斯
【问题讨论】:
标签: python python-3.x pyqt pyqt5 qmessagebox