3 import wx
.lib
.mixins
.listctrl
as listmix
7 class BoldedStaticText(wx
.StaticText
):
8 def __init__(self
, *args
, **kwargs
):
9 wx
.StaticText
.__init
__(self
, *args
, **kwargs
)
11 font
.SetWeight(wx
.BOLD
)
15 class ListCtrl(wx
.ListCtrl
, listmix
.ListCtrlAutoWidthMixin
):
16 def __init__(self
, parent
, size
=wx
.DefaultSize
, style
=0):
17 wx
.ListCtrl
.__init
__(self
, parent
, size
=size
, style
=style
)
18 listmix
.ListCtrlAutoWidthMixin
.__init
__(self
)
21 class ErrorPanel(wx
.Panel
):
22 def __init__(self
, parent
):
23 wx
.Panel
.__init
__(self
, parent
)
25 self
.box
= wx
.BoxSizer(wx
.VERTICAL
)
28 self
.box
.Add(BoldedStaticText(self
, label
="An error has occurred")
29 , 0, wx
.ALIGN_CENTER | wx
.ALL
, 10)
31 # Exception Information
32 boxInfoGrid
= wx
.FlexGridSizer(2, 2, 0, 0)
33 textFlags
= wx
.ALIGN_LEFT | wx
.LEFT | wx
.RIGHT | wx
.TOP
35 boxInfoGrid
.Add(BoldedStaticText(self
, label
="Type: "), 0, textFlags
, 5 )
36 self
.error_type
= wx
.StaticText(self
, label
="")
37 boxInfoGrid
.Add(self
.error_type
, 0, textFlags
, 5 )
39 textFlags
= wx
.ALIGN_LEFT | wx
.ALL
40 boxInfoGrid
.Add(BoldedStaticText(self
, label
="Value: ") , 0, textFlags
, 5 )
41 self
.error_details
= wx
.StaticText(self
, label
="")
42 boxInfoGrid
.Add(self
.error_details
, 0, textFlags
, 5 )
44 bbox
= wx
.BoxSizer(wx
.HORIZONTAL
)
45 self
.copy
= wx
.Button(self
, label
="Copy to Clipboard")
46 self
.close
= wx
.Button(self
, label
="&Close")
50 # Set up the traceback list
51 # This one automatically resizes last column to take up remaining space
52 self
.__list
= ListCtrl(self
, style
=wx
.LC_REPORT | wx
.SUNKEN_BORDER
)
53 self
.__list
.InsertColumn(0, "Filename")
54 self
.__list
.InsertColumn(1, "Line", wx
.LIST_FORMAT_RIGHT
)
55 self
.__list
.InsertColumn(2, "Function")
56 self
.__list
.InsertColumn(3, "Code")
58 self
.box
.Add(boxInfoGrid
)
60 self
.box
.Add(BoldedStaticText(self
, label
="Traceback:")
61 , 0, wx
.ALIGN_LEFT | wx
.LEFT | wx
.TOP
, 5)
62 self
.box
.Add(self
.__list
, 1, wx
.EXPAND | wx
.ALIGN_CENTER | wx
.ALL
, 5)
63 self
.box
.Add(bbox
, 0, wx
.ALIGN_RIGHT | wx
.ALL
, 5)
66 self
.SetSizer(self
.box
)
70 self
.copy
.Bind(wx
.EVT_BUTTON
, self
.OnCopy
)
74 return repr(val
)[1:-1]
76 def show(self
, type_
, value
, tb
):
77 sys
.__excepthook
__(type_
, value
, tb
)
79 self
.error_type
.SetLabel(self
.repr_(type_
))
80 self
.error_details
.SetLabel(repr(value
))
82 self
.tb_info
= traceback
.format_exception(type_
, value
, tb
)
84 self
.__list
.DeleteAllItems()
85 for filename
, line
, func
, text
in traceback
.extract_tb(tb
):
86 self
.__list
.InsertStringItem(x
,
87 self
.repr_(os
.path
.basename(filename
))) # Filename
88 self
.__list
.SetStringItem(x
, 1, unicode(line
)) # Line
89 self
.__list
.SetStringItem(x
, 2, unicode(func
)) # Function
90 self
.__list
.SetStringItem(x
, 3, self
.repr_(text
)) # Code
95 def OnCopy(self
, event
):
96 data
= wx
.TextDataObject()
97 data
.SetText(''.join(self
.tb_info
))
98 if wx
.TheClipboard
.Open():
99 wx
.TheClipboard
.SetData(data
)
100 wx
.TheClipboard
.Close()
101 event
.GetEventObject().Enable(False)
103 wx
.MessageBox("Unable to open the clipboard", "Error")
107 def __init__(self
, parent
=None):
111 def __call__(self
, type_
, value
, tb
):
113 self
.frame
= wx
.Frame(self
.parent
, size
=(600,400))
114 self
.error_panel
= ErrorPanel(self
.frame
)
116 self
.error_panel
.close
.Bind(wx
.EVT_BUTTON
, self
.OnClose
)
117 self
.frame
.Bind(wx
.EVT_CLOSE
, self
.OnClose
)
119 self
.error_panel
.show(type_
, value
, tb
)
121 self
.frame
.CenterOnParent()
124 def OnClose(self
, event
):
129 if __name__
== '__main__':
131 raise Exception("Teste")
135 sys
.excepthook
= ErrorFrame(f
)
137 wx
.FutureCall(500, raise_
)