Python 中的崩溃报告
Is there a crash reporting framework that can be used for pure Python Tkinter applications? Ideally, it should work cross-platform.
Practically speaking, this is more of 'exception reporting' since the Python interpreter itself hardly crashes.
Here's a sample crash reporter:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该通过设置 sys.excepthook 来实现您自己的 except 钩子,而不是到处使用
try.. except
来污染您的代码。下面是一个示例:在应用程序启动时调用
install_exception()
。ErrorReportDialog
是我制作的一个 Qt 对话框。traceback.format_exception()
将以与 Python 解释器中相同的方式格式化传递给 except 钩子的参数。编辑:我忘了提到一个小问题。它不适用于线程(好吧,至少我上次检查时没有)。对于在另一个线程中运行的代码,您需要将其包装在
try.. except
块中。Rather than polluting your code with
try..except
everywhere, you should just implement your own except hook by settingsys.excepthook
. Here is an example:Call
install_exception()
when your application starts.ErrorReportDialog
is a Qt dialog I've made.traceback.format_exception()
will format argument passed to the except hook in the same way it does in Python's interpreter.EDIT: I forgot to mention a little gotcha with that. It doesn't work with threads (well, at least it didn't last time I checked). For code running in another thread, you will need to wrap it in a
try..except
block.坚持尝试,除非应用程序可能崩溃(I/O、网络等)。每当调用 except 时,调用一个函数来杀死旧窗口,生成一个新的 tkinter 通知窗口,或者一个 自定义一个并包含您的错误消息。
对新窗口执行 root.after 并发送错误报告 (urllib)。
如果您愿意,可以放置重新启动按钮。
没有崩溃报告框架 - 因为 tkinter 不是那种类型的 GUI。它几乎是简单命令行应用程序的包装。
如果您想要上面屏幕截图中看到的功能,请使用 pyqt/gtk 或 wxpython。但我很确定,无论你走到哪里,你都必须写自己的记者。
Stick try excepts everywhere your application can crash (I/O, networking etc.). Whenever an except is called, call a function that will kill the old window, spawn a new tkinter notification window, or a custom one with your error message.
Do a root.after to the new window and send your error report (urllib).
Put a restart button if you wish.
There is no crash reporting framework - as tkinter is not that type of GUI. It's pretty much a wrapper for simple command line apps.
Go pyqt/gtk or wxpython if you want the features seen in the screen-shot above. But I'm pretty sure that where ever you go, you'll have to write your own reporter.