python 3.4、设置QWidgets.QMessageBox
python 3.4, setting up QWidgets.QMessageBox
我的代码更新,基于 Israel Unterman 的回复:
错误-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')
如果我使用的是普通代码(实际上是相同的代码,只是包裹在 class 中),消息会出现,然后在
行
id = give_id(self)
代码停止时没有来自 python 的任何错误消息,只是:
Process finished with exit code 1
如果我使用测试脚本,没有什么比这个(没有 QMessageBox!):
Process finished with exit code 1
如果我调试代码,init() 得到相同的对象和变量,但是
super().__init__(parent)
失败,没有任何消息。
那么错误或不同之处在哪里呢
这里是 Class 的简化版本(这里显示所有代码太长了),"Error" 几乎可以正常工作:
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')
这是我的错误处理模块
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)
感谢您的帮助
詹姆斯
您没有创建消息框的对象。要创建对象,请使用:
msg_box = QMessageBox()
但是您不需要经历所有这些,因为 QMessageBox
具有用于显示消息的静态函数,您可以直接在 QMessageBox
class 上调用这些函数。例如:
QMessageBox.warning(None, 'title', 'msg')
您还可以对按钮进行一些控制,请参阅 QMessageBox
我的代码更新,基于 Israel Unterman 的回复:
错误-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')
如果我使用的是普通代码(实际上是相同的代码,只是包裹在 class 中),消息会出现,然后在
行id = give_id(self)
代码停止时没有来自 python 的任何错误消息,只是:
Process finished with exit code 1
如果我使用测试脚本,没有什么比这个(没有 QMessageBox!):
Process finished with exit code 1
如果我调试代码,init() 得到相同的对象和变量,但是
super().__init__(parent)
失败,没有任何消息。 那么错误或不同之处在哪里呢
这里是 Class 的简化版本(这里显示所有代码太长了),"Error" 几乎可以正常工作:
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')
这是我的错误处理模块
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)
感谢您的帮助
詹姆斯
您没有创建消息框的对象。要创建对象,请使用:
msg_box = QMessageBox()
但是您不需要经历所有这些,因为 QMessageBox
具有用于显示消息的静态函数,您可以直接在 QMessageBox
class 上调用这些函数。例如:
QMessageBox.warning(None, 'title', 'msg')
您还可以对按钮进行一些控制,请参阅 QMessageBox