wxPython不会抛出异常,而是提供原始错误消息

时间:2022-10-16 07:12:34

I'm coding the menu for an application I'm writing in python, using wxPython libraries for the user interface, and I'm attempting to add icons to some of the menu items. Because I'm trying to be conscientious about it, I'm trying to limit the damage done if one of the image files referenced doesn't exist, and the most simple method (in my mind) is to use exceptions.

我正在为我正在用python编写的应用程序编写菜单,使用wxPython库作为用户界面,我正在尝试向某些菜单项添加图标。因为我正在努力做到尽职尽责,所以如果其中一个引用的图像文件不存在,我试图限制所造成的损害,而最简单的方法(在我看来)是使用异常。

The trouble is, when I link to a file that doesn't exist, an exception isn't thrown. Instead I get a horrific message box stating:

问题是,当我链接到不存在的文件时,不会抛出异常。相反,我得到一个可怕的消息框说明:

Can't load image from <path>: Image does not exist.

无法从 加载图像:图像不存在。

This message is exactly the type of thing I'm trying to stop, but even with the broadest exception catching statement nothing works.

这条消息正是我试图阻止的类型,但即使有最广泛的异常捕获语句也无效。

This is a cut down version, taking what seems to be relevant, from what I've written:

这是一个缩减版本,从我写的内容中看出了相似的内容:

NewProject = wx.MenuItem(File, -1, "&New Project\tCtrl+N", "Create a new project")
try:
    # raises an error message but not an exception
    NewProject.SetBitmap(wx.Image( <path>  ), wx.BITMAP_TYPE_PNG).ConvertToBitmap())
except Exception:
    pass

So these are my questions: What am I doing wrong? Am I approaching this in the wrong direction, putting too much emphasis on exceptions, when there are other ways around it (though non that seem in my head to be as simple)? Is this a bug in the wxPython library, since I'm pretty sure it should be throwing an exception even if it's not the best way around it?

所以这些是我的问题:我做错了什么?我是否朝着错误的方向接近这一点,过分强调异常,当有其他方法时(虽然看起来不那么简单)?这是wxPython库中的一个错误,因为我很确定它应该抛出异常,即使它不是最好的方法吗?

p.s. The best a Google search could do was recommend I converted all the images to python code using the img2py module that comes with wxPython, but I would prefer to keep the images in image format for what I'm doing.

附:谷歌搜索可以做的最好的建议是我使用wxPython附带的img2py模块将所有图像转换为python代码,但我更喜欢将图像保留为图像格式以用于我正在做的事情。

1 个解决方案

#1


That is a known issue. Robin Dunn answered it a couple of times: just create your Logging method eg.:

这是一个已知问题。 Robin Dunn回答了几次:只需创建你的Logging方法,例如:

dummy_log=wx.LogNull()

when the variable dummy_log runs out of scope, normal logging is enabled again.

当变量dummy_log超出范围时,将再次启用正常日志记录。

#1


That is a known issue. Robin Dunn answered it a couple of times: just create your Logging method eg.:

这是一个已知问题。 Robin Dunn回答了几次:只需创建你的Logging方法,例如:

dummy_log=wx.LogNull()

when the variable dummy_log runs out of scope, normal logging is enabled again.

当变量dummy_log超出范围时,将再次启用正常日志记录。