473,563 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sys.excepthack. ..

Becoming a fan of wxPython, but I can't stand
what it does with error messsages (I can't find
a way to dismiss that window with the error message
from the keyboard. Seems to be anti-modal - the
key strokes that normally kill the active window
kill the main window (sitting behind the window
with the error message) instead. If the window
only had an OK button...)

So I want to pop up a modal dialog on error.

Tried setting sys.stderr to an object with a
write method that pops up a dialog. The problem
with that is write() gets called several times
per exception - I don't see how to get my new
sys.stderr object to figure out when it's time
to show the message.

So then I found sys.excepthook. The problem with
that was that I was too stoopid to figure out how
to get a readable error message from the parameters
passed to excepthook.

Came up with a ridiculous hack involving both sys.stderr
and sys.excepthook. Works exactly the way I want.
Seems ridiculous - what's the right way to do this?

Ridiculous_hack .py:

import sys
import wx

def hook(*args):
try:
sys.__excepthoo k__(*args)
finally:
printer.Show()

class ErrorDisplay:
def __init__(self):
self.buffer = ''
def write(self, text):
self.buffer = self.buffer + text

def Show(self):
wx.MessageDialo g(None, str(self.buffer ),
'Error:',wx.OK) .ShowModal()
self.buffer = ''

printer = ErrorDisplay()
sys.stderr = printer
sys.excepthook = hook

--
David C. Ullrich
Jun 27 '08 #1
1 1426
David C. Ullrich <du******@spryn et.comwrote:
Becoming a fan of wxPython, but I can't stand
what it does with error messsages
[snip]
So I want to pop up a modal dialog on error.
[snip]
Came up with a ridiculous hack involving both sys.stderr
and sys.excepthook. Works exactly the way I want.
Seems ridiculous - what's the right way to do this?

Ridiculous_hack .py:

import sys
import wx

def hook(*args):
try:
sys.__excepthoo k__(*args)
finally:
printer.Show()

class ErrorDisplay:
def __init__(self):
self.buffer = ''
def write(self, text):
self.buffer = self.buffer + text

def Show(self):
wx.MessageDialo g(None, str(self.buffer ),
'Error:',wx.OK) .ShowModal()
self.buffer = ''

printer = ErrorDisplay()
sys.stderr = printer
sys.excepthook = hook
Here is how I've done it in the past. It is a complete runnable
example :-

------------------------------------------------------------
#!/usr/bin/python

import wx
import sys
from traceback import format_exceptio n

class TestApp(wx.Fram e):
"""Test error handling"""

def __init__(self, parent, id, title):
wx.Frame.__init __(self, parent, -1, title, size=(200,200))
sys.excepthook = self.OnExceptio n
button = wx.Button(self, wx.NewId(), "Produce Error")
self.Bind(wx.EV T_BUTTON, self.OnClick)
self.Show(True)

def OnException(sel f, type, value, traceback):
"""
Called on OnException
"""
error_text = "".join(format_ exception(type, value, traceback))
wx.MessageDialo g(None, error_text, 'Custom Error:', wx.OK).ShowModa l()

def OnClick(self, evt):
"Click with a deliberate mistake"
adsfsfsdf

if __name__ == "__main__":
app = wx.PySimpleApp( )
frame = TestApp(None, -1, "Test")
app.MainLoop()
------------------------------------------------------------

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
873
by: Jean-Paul Calderone | last post by:
On Wed, 14 May 2008 15:47:18 -0500, "David C. Ullrich" <dullrich@sprynet.comwrote: Hi David, Take a look at the traceback module. The excepthook gets called with an exception type, instance, and traceback object. The traceback module is good at turning these things into strings. Jean-Paul
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.