显示弹出消息以在代码停止时显示错误消息 运行 WXPYTHON

Showing pop up message to show the error message when the code stops running WXPYTHON

我是wxpython的新手。有没有办法在代码停止时显示弹出消息以显示错误消息运行?所以用户不需要查看终端就可以看到代码实际上停止了。

谢谢!

你试过 wxPython 对话框吗?我认为这是显示警报消息的最简单方法。

import wx

app = wx.App()
wx.MessageBox('Your error message', 'Your error title', wx.OK | wx.ICON_ERROR)

更多信息:

https://wxpython.org/Phoenix/docs/html/wx.MessageDialog.html

https://pythonspot.com/wxpython-dialogs/

只是为了说明@Michael Butscher 的评论和@Dan A.S 的回答。

您可以使用 systracebackwx.MessageDialog

中捕获和显示事件
import wx
import sys, traceback

def my_message(exception_type, exception_value, exception_traceback):
    msg = "Oh no! An error has occurred.\n\n"
    tb= traceback.format_exception(exception_type, exception_value, exception_traceback)
    for i in tb:
        msg += i
    dlg=wx.MessageDialog(None, msg, str(exception_type), wx.OK|wx.ICON_INFORMATION)
    dlg.ShowModal()
    dlg.Destroy()

sys.excepthook = my_message

class MyFrame(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="", size=(360,100)):
        super(MyFrame, self).__init__(parent, id, title, size)
        self.panel = wx.Panel(self)
        self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKey)
        self.Show()

    def OnKey(self, event):
        print ("alpha" + 1)

if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame(None,title="Press a key")
    app.MainLoop()