473,699 Members | 2,234 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 1434
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
878
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
8697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9045
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8892
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7761
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5878
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.