如何使用wxPython在Windows中使用本机错误图标和错误声音?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用wxPython在Windows中使用本机错误图标和错误声音?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4189字,纯文字阅读大概需要6分钟。
内容图文
![如何使用wxPython在Windows中使用本机错误图标和错误声音?](/upload/InfoBanner/zyjiaocheng/948/4137dff77a6041738f4b23f0deee89a8.jpg)
这条简单的线:
wx.MessageBox('Foo', 'Bar', wx.OK | wx.ICON_ERROR)
给我一个带有错误图标和Windows错误噪声的消息框(这与wx.Bell()不同).我想为未捕获的异常创建一个自定义错误对话框,在文本控件等中可以使用追溯功能,并且我想同时包含Windows错误图标和噪音.我知道这两种Windows版本之间都不同,甚至可以自定义错误噪声.
是否可以通过wxPython直接使用这些本机Windows资源?奖金问题;如果答案是否定的,那么做我想做的最直接的方法是什么?
接受答案后的结果:
我只是想在Anonymous Coward出色回答之后展示结果,因为它们远远超出了我的期望.这是错误对话框,现在针对未处理的异常(在Windows 8上)弹出:
它还包含现代Windows“ UNNK!”.错误的声音.这是对话框后面的代码.我将其放在一个单独的模块中,该模块在导入时覆盖sys.excepthook:
"""This module, when imported, overrides the default unhandled exception hook
with one that displays a fancy wxPython error dialog."""
import sys
import textwrap
import traceback
import winsound
import wx
def custom_excepthook(exception_type, value, tb):
dialog = ExceptionDialog(exception_type, value, tb)
dialog.ShowModal()
# Override sys.excepthook
sys.excepthook = custom_excepthook
class ExceptionDialog(wx.Dialog):
"""This class displays an error dialog with details information about the
input exception, including a traceback."""
def __init__(self, exception_type, exception, tb):
wx.Dialog.__init__(self, None, -1, title="Unhandled error",
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
self.SetSize((640, 480))
self.SetMinSize((420, 200))
self.exception = (exception_type, exception, tb)
self.initialize_ui()
winsound.MessageBeep(winsound.MB_ICONHAND)
def initialize_ui(self):
extype, exception, tb = self.exception
panel = wx.Panel(self, -1)
# Create the top row, containing the error icon and text message.
top_row_sizer = wx.BoxSizer(wx.HORIZONTAL)
error_bitmap = wx.ArtProvider.GetBitmap(
wx.ART_ERROR, wx.ART_MESSAGE_BOX
)
error_bitmap_ctrl = wx.StaticBitmap(panel, -1)
error_bitmap_ctrl.SetBitmap(error_bitmap)
message_text = textwrap.dedent("""\
I'm afraid there has been an unhandled error. Please send the
contents of the text control below to the application's developer.\
""")
message_label = wx.StaticText(panel, -1, message_text)
top_row_sizer.Add(error_bitmap_ctrl, flag=wx.ALL, border=10)
top_row_sizer.Add(message_label, flag=wx.ALIGN_CENTER_VERTICAL)
# Create the text control with the error information.
exception_info_text = textwrap.dedent("""\
Exception type: {}
Exception: {}
Traceback:
{}\
""")
exception_info_text = exception_info_text.format(
extype, exception, ''.join(traceback.format_tb(tb))
)
text_ctrl = wx.TextCtrl(panel, -1,
style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
text_ctrl.SetValue(exception_info_text)
# Create the OK button in the bottom row.
ok_button = wx.Button(panel, -1, 'OK')
self.Bind(wx.EVT_BUTTON, self.on_ok, source=ok_button)
ok_button.SetFocus()
ok_button.SetDefault()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(top_row_sizer)
# sizer.Add(message_label, flag=wx.ALL | wx.EXPAND, border=10)
sizer.Add(text_ctrl, proportion=1, flag=wx.EXPAND)
sizer.Add(ok_button, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
panel.SetSizer(sizer)
def on_ok(self, event):
self.Destroy()
我唯一希望做的改进就是使静态文本根据对话框的宽度自动流动并自动换行,但是我不必为此而创建一个自定义控件类.
解决方法:
wx.MessageBox调用特定于操作系统的标准消息/警报对话框(在Windows上,它是user32.dll的MessageBox()). wxWidgets只是将您提供的标志(例如wx.OK和wx.ICON_INFORMATION)转换为特定于操作系统的标志和本机消息框的选项.
您可以通过wx.ArtProvider获取特定于操作系统的图标.
就声音而言,wx.Sound只能播放声音文件.但是,如果应用程序不必是可移植的,则可以使用winsound模块.
import wx
import winsound # windows only
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,wx.ID_ANY)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add( wx.StaticBitmap(self,bitmap=wx.ArtProvider.GetBitmap(wx.ART_INFORMATION)) )
sizer.Add( wx.StaticBitmap(self,bitmap=wx.ArtProvider.GetBitmap(wx.ART_QUESTION)) )
sizer.Add( wx.StaticBitmap(self,bitmap=wx.ArtProvider.GetBitmap(wx.ART_WARNING)) )
sizer.Add( wx.StaticBitmap(self,bitmap=wx.ArtProvider.GetBitmap(wx.ART_ERROR)) )
self.SetSizerAndFit(sizer)
self.Show()
winsound.MessageBeep(winsound.MB_ICONASTERISK)
winsound.PlaySound('SystemHand', winsound.SND_ASYNC | winsound.SND_ALIAS)
app = wx.PySimpleApp()
Frame()
app.MainLoop()
内容总结
以上是互联网集市为您收集整理的如何使用wxPython在Windows中使用本机错误图标和错误声音?全部内容,希望文章能够帮你解决如何使用wxPython在Windows中使用本机错误图标和错误声音?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。