Attachment 'Image2PyFile_GUI.py'
Download 1 """
2 Sample basic GUI app demo that invokes Image2PyFile.PY
3
4 Passee on command line args to Image2PyFile.PY and display the results in a simple GUI.
5 Mesages that are progress, actions, warnings and notes are shown in the left pane.
6 Failure and error messages are shown on the right.
7
8 Ray Pasco
9 pascor(at)verizon(dot)net
10
11 Version
12 1.2.1 2012-04-29 The error textCtrl is now located underneath the stdout textCtrl.
13 1.2 2012-04-26 Fixed bugs, added -Q, enable all messages option.
14
15 """
16
17 import sys, os
18 import wx
19
20 import Image2PyFile as i2p
21
22 #------------------------------------------------------------------------------
23
24 class RedirectOutput() :
25 """
26 Redirect print and write output to any control implementing method WriteText.
27
28 Usage:
29 stdoutTc = wx.TextCtrl( tcParent )
30
31 sysStdoutSave = sys.stdout
32 redirStd = RedirectOutput( stdoutTc )
33 sys.stdout = redirStd
34
35 ...
36
37 # Undo the output redirection, if desired.
38 sys.stdout = sysStdoutSave
39 """
40
41 # Created : Mike Driscoll
42 # Contact : mike@pythonlibrary.org
43 # Website : ???
44
45 def __init__( self, textDisplayControl ) :
46 self.textOutWidget = textDisplayControl
47
48 def write( self, s ) :
49 self.textOutWidget.WriteText( s )
50
51 #end RedirectOutput class
52
53 DEBUG_REDIRECT_OUTPUT_TO_SHELL = 0
54
55 #------------------------------------------------------------------------------
56 #===============================================================================
57 #------------------------------------------------------------------------------
58
59 class AppFrame( wx.Frame ) :
60
61 def __init__( self, args ) :
62 DEBUG = 0
63
64 wx.Frame.__init__( self, None, wx.ID_ANY, title='Image2PyFile GUI Demo' )
65 self.SetClientSize( (750, 300) )
66 self.SetPosition( (100, 0) )
67
68 # Add a panel so the Frame looks and acts the same on all platforms.
69 frmPanel = wx.Panel( self, -1 )
70
71 # This implements an alternate very easy way to exist this app.
72 frmPanel.Bind( wx.EVT_LEFT_DCLICK, self.OnExit )
73
74 #-----
75
76 stdoutTc_vertSizer = wx.BoxSizer( wx.VERTICAL )
77
78 stdoutTc_stTxt = wx.StaticText( frmPanel, -1, 'Successful Operations' )
79 stdoutTc_vertSizer.Add( stdoutTc_stTxt, 0, wx.ALL|wx.EXPAND, 5 )
80
81 sharedTcStyle = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL|wx.HSCROLL| wx.TE_RICH2
82
83 stdoutTc = wx.TextCtrl( frmPanel, wx.NewId(), style=sharedTcStyle )
84 stdoutTc.Bind( wx.EVT_LEFT_DCLICK, self.OnExit )
85 # Create a monospaced font from the default.
86 self.SetMonospaceFont( stdoutTc )
87
88 stdoutTc_vertSizer.Add( stdoutTc, 1, wx.ALL|wx.EXPAND, 5 )
89
90 #-----
91
92 stderrTc_vertSizer = wx.BoxSizer( wx.VERTICAL )
93
94 stderrTc_stTxt = wx.StaticText( frmPanel, -1, 'Failed Operations' )
95 stderrTc_vertSizer.Add( stderrTc_stTxt, 0, wx.ALL|wx.EXPAND, 5 )
96
97 stderrTc = wx.TextCtrl( frmPanel, wx.NewId(), style=sharedTcStyle )
98 stderrTc.Bind( wx.EVT_LEFT_DCLICK, self.OnExit )
99 # Create a monospaced font from the default.
100 self.SetMonospaceFont( stderrTc )
101
102 stderrTc_vertSizer.Add( stderrTc, 1, wx.ALL|wx.EXPAND, 5 )
103
104 # Exit Button
105 exitBtn = wx.Button( frmPanel, wx.ID_ANY, 'Exit' )
106 exitBtn.Bind( wx.EVT_BUTTON, self.OnExit)
107
108 # An overall sizer for the Frame client contents
109 frmPanel_vertSizer = wx.BoxSizer( wx.VERTICAL )
110
111 frmPanel_vertSizer.Add( stdoutTc_vertSizer, 1, wx.ALL|wx.EXPAND, 5 )
112 frmPanel_vertSizer.Add( stderrTc_vertSizer, 1, wx.ALL|wx.EXPAND, 5 )
113
114 frmPanel_vertSizer.Add( exitBtn, 0, wx.CENTER, 5 )
115 frmPanel_vertSizer.Add( (10, 10) )
116 frmPanel.SetSizer( frmPanel_vertSizer )
117
118 #-----
119
120 if DEBUG_REDIRECT_OUTPUT_TO_SHELL : # DEBUG
121
122 pass # All output will normally go into textCtrl's by default.
123
124 else :
125
126 # Redirect all text output from Image2PyFile.py to the textCtrl's.
127 stdoutOrig, stderrOrig = sys.stdout, sys.stderr
128
129 sys.stdout = RedirectOutput( stdoutTc )
130 sys.stderr = RedirectOutput( stderrTc )
131
132 #end if
133
134 #-----
135
136 # Process the command line, process and display the results.
137 i2p.Image2PyFile( args ) # All output info text will appear in the GUI.
138
139 stdoutTc.SetInsertionPoint( 0 ) # Move the cursor back to the first line.
140 stderrTc.SetInsertionPoint( 0 )
141
142 # Show the error/failure messages pane if there are any messages.
143 if (stderrTc.GetNumberOfLines() > 1) : # Value of 1 == no lines of text.
144
145 stderrTc_vertSizer.ShowItems( True ) # Make the err panel controls appear.
146
147 # Resize the Frame's client area to also show TextCtrl.
148 clientSizeX, clientSizeY = self.GetClientSize()
149 stdoutTcWid, stdoutTcHgt = stdoutTc_vertSizer.GetSize()
150 # Make more room to display the error staticText and its textCtrl.
151 self.SetClientSize( (clientSizeX, clientSizeY+275) )
152
153 else : # Err messages panel will not be visible if there was no output to stderr.
154 stderrTc_vertSizer.ShowItems( False )
155 #end if
156 frmPanel_vertSizer.Layout()
157
158 self.Show()
159
160 #end __init__
161
162 #------------------------
163
164 def OnExit( self, event ) :
165 """A "hook" to allow future cleanup operations to be performed."""
166
167 self.Destroy()
168
169 #------------------------
170
171 def SetMonospaceFont( self, ctrl ) :
172
173 # Retrieve the current font so we can use maost of its characteristics.
174 ctrlFont = ctrl.GetFont()
175
176 # Args: wx.Font( pointSize, family, style, weight, underline, face, encoding )
177
178 # Set the horizontal size in pixels, NOT points ! Vert size is about 11 pixels/char.
179 fontSize = 9
180 if os.name == 'nt' : # A more vertically compact font. Same horizontal spacing, though.
181 ctrlFont = wx.Font( fontSize, wx.FONTFAMILY_MODERN, ctrlFont.GetStyle(),
182 ctrlFont.GetWeight(), ctrlFont.GetUnderlined(), "Lucida Console" )
183 else :
184 ctrlFont = wx.Font( fontSize, wx.FONTFAMILY_MODERN, ctrlFont.GetStyle(),
185 ctrlFont.GetWeight(), ctrlFont.GetUnderlined(), str(ctrlFont.GetEncoding()) )
186 #end if
187 ctrl.SetFont( ctrlFont )
188
189 #end def
190
191 #end AppFrame class
192
193 #==============================================================================
194
195 if __name__ == '__main__' :
196
197 args = sys.argv[ 1: ]
198
199 myApp = wx.App( redirect=True )
200 appFrame = AppFrame( args )
201 myApp.MainLoop()
202
203 #end if
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.