4 This module implements the SuperDoodle demo application. It takes the
5 DoodleWindow previously presented and reuses it in a much more
6 intelligent Frame. This one has a menu and a statusbar, is able to
7 save and reload doodles, clear the workspace, and has a simple control
8 panel for setting color and line thickness in addition to the popup
9 menu that DoodleWindow provides. There is also a nice About dialog
10 implmented using an wxHtmlWindow.
13 from wxPython
.wx
import *
14 from doodle
import DoodleWindow
19 #----------------------------------------------------------------------
30 class DoodleFrame(wxFrame
):
32 A DoodleFrame contains a DoodleWindow and a ControlPanel and manages
33 their layout with a wxBoxSizer. A menu and associated event handlers
34 provides for saving a doodle to a file, etc.
37 def __init__(self
, parent
):
38 wxFrame
.__init
__(self
, parent
, -1, self
.title
, size
=(800,600),
39 style
=wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE
)
40 self
.CreateStatusBar()
44 self
.doodle
= DoodleWindow(self
, -1)
45 cPanel
= ControlPanel(self
, -1, self
.doodle
)
47 # Create a sizer to layout the two windows side-by-side.
48 # Both will grow vertically, the doodle window will grow
49 # horizontally as well.
50 box
= wxBoxSizer(wxHORIZONTAL
)
51 box
.Add(cPanel
, 0, wxEXPAND
)
52 box
.Add(self
.doodle
, 1, wxEXPAND
)
54 # Tell the frame that it should layout itself in response to
56 self
.SetAutoLayout(true
)
62 data
= self
.doodle
.GetLinesData()
63 f
= open(self
.filename
, 'w')
71 f
= open(self
.filename
, 'r')
72 data
= cPickle
.load(f
)
74 self
.doodle
.SetLinesData(data
)
75 except cPickle
.UnpicklingError
:
76 wxMessageBox("%s is not a doodle file." % self
.filename
,
77 "oops!", style
=wxOK|wxICON_EXCLAMATION
)
81 # create the file menu
83 menu1
.Append(idOPEN
, "&Open", "Open a doodle file")
84 menu1
.Append(idSAVE
, "&Save", "Save the doodle")
85 menu1
.Append(idSAVEAS
, "Save &As", "Save the doodle in a new file")
86 menu1
.AppendSeparator()
87 menu1
.Append(idCLEAR
, "&Clear", "Clear the current doodle")
88 menu1
.AppendSeparator()
89 menu1
.Append(idEXIT
, "E&xit", "Terminate the application")
93 menu2
.Append(idABOUT
, "&About", "Display the gratuitous 'about this app' thingamajig")
95 # and add them to a menubar
97 menuBar
.Append(menu1
, "&File")
98 menuBar
.Append(menu2
, "&Help")
99 self
.SetMenuBar(menuBar
)
101 EVT_MENU(self
, idOPEN
, self
.OnMenuOpen
)
102 EVT_MENU(self
, idSAVE
, self
.OnMenuSave
)
103 EVT_MENU(self
, idSAVEAS
, self
.OnMenuSaveAs
)
104 EVT_MENU(self
, idCLEAR
, self
.OnMenuClear
)
105 EVT_MENU(self
, idEXIT
, self
.OnMenuExit
)
106 EVT_MENU(self
, idABOUT
, self
.OnMenuAbout
)
110 wildcard
= "Doodle files (*.ddl)|*.ddl|All files (*.*)|*.*"
112 def OnMenuOpen(self
, event
):
113 dlg
= wxFileDialog(self
, "Open doodle file...", os
.getcwd(),
114 style
=wxOPEN
, wildcard
= self
.wildcard
)
115 if dlg
.ShowModal() == wxID_OK
:
116 self
.filename
= dlg
.GetPath()
118 self
.SetTitle(self
.title
+ ' -- ' + self
.filename
)
122 def OnMenuSave(self
, event
):
123 if not self
.filename
:
124 self
.OnMenuSaveAs(event
)
129 def OnMenuSaveAs(self
, event
):
130 dlg
= wxFileDialog(self
, "Save doodle as...", os
.getcwd(),
131 style
=wxSAVE | wxOVERWRITE_PROMPT
,
132 wildcard
= self
.wildcard
)
133 if dlg
.ShowModal() == wxID_OK
:
134 filename
= dlg
.GetPath()
135 if not os
.path
.splitext(filename
)[1]:
136 filename
= filename
+ '.ddl'
137 self
.filename
= filename
139 self
.SetTitle(self
.title
+ ' -- ' + self
.filename
)
143 def OnMenuClear(self
, event
):
144 self
.doodle
.SetLinesData([])
145 self
.SetTitle(self
.title
)
148 def OnMenuExit(self
, event
):
152 def OnMenuAbout(self
, event
):
153 dlg
= DoodleAbout(self
)
159 #----------------------------------------------------------------------
162 class ControlPanel(wxPanel
):
164 This class implements a very simple control panel for the DoodleWindow.
165 It creates buttons for each of the colours and thickneses supported by
166 the DoodleWindow, and event handlers to set the selected values. There is
167 also a little window that shows an example doodleLine in the selected
168 values. Nested sizers are used for layout.
170 def __init__(self
, parent
, ID
, doodle
):
171 wxPanel
.__init
__(self
, parent
, ID
, style
=wxRAISED_BORDER
)
176 # Make a grid of buttons for each colour. Attach each button
177 # event to self.OnSetColour. The button ID is the same as the
178 # key in the colour dictionary.
179 colours
= doodle
.menuColours
180 keys
= colours
.keys()
182 cGrid
= wxGridSizer(cols
=numCols
, hgap
=2, vgap
=2)
184 bmp
= self
.MakeBitmap(wxNamedColour(colours
[k
]))
185 b
= wxBitmapButton(self
, k
, bmp
)
186 EVT_BUTTON(self
, k
, self
.OnSetColour
)
189 # Save the button size so we can use it for the number buttons
190 btnSize
= b
.GetSize()
192 # Make a grid of buttons for the thicknesses. Attach each button
193 # event to self.OnSetThickness. The button ID is the same as the
195 tGrid
= wxGridSizer(cols
=numCols
, hgap
=2, vgap
=2)
196 for x
in range(1, doodle
.maxThickness
+1):
197 b
= wxButton(self
, x
, str(x
), size
=btnSize
)
198 EVT_BUTTON(self
, x
, self
.OnSetThickness
)
201 # Make a colour indicator window, it is registerd as a listener
202 # with the doodle window so it will be notified when the settings
204 ci
= ColourIndicator(self
)
205 doodle
.AddListener(ci
)
209 # Make a box sizer and put the two grids and the indicator
211 box
= wxBoxSizer(wxVERTICAL
)
212 box
.Add(cGrid
, 0, wxALL
, spacing
)
213 box
.Add(tGrid
, 0, wxALL
, spacing
)
214 box
.Add(ci
, 0, wxEXPAND|wxALL
, spacing
)
216 self
.SetAutoLayout(true
)
218 # Resize this window so it is just large enough for the
219 # minimum requirements of the sizer.
224 def MakeBitmap(self
, colour
):
226 We can create a bitmap of whatever we want by simply selecting
227 it into a wxMemoryDC and drawing on it. In this case we just set
228 a background brush and clear the dc.
230 bmp
= wxEmptyBitmap(16,16)
233 dc
.SetBackground(wxBrush(colour
))
235 dc
.SelectObject(wxNullBitmap
)
239 def OnSetColour(self
, event
):
241 Use the event ID to get the colour, set that colour in the doodle.
243 colour
= self
.doodle
.menuColours
[event
.GetId()]
244 self
.doodle
.SetColour(colour
)
247 def OnSetThickness(self
, event
):
249 Use the event ID to set the thickness in the doodle.
251 self
.doodle
.SetThickness(event
.GetId())
254 #----------------------------------------------------------------------
256 class ColourIndicator(wxWindow
):
258 An instance of this class is used on the ControlPanel to show
259 a sample of what the current doodle line will look like.
261 def __init__(self
, parent
):
262 wxWindow
.__init
__(self
, parent
, -1, style
=wxSUNKEN_BORDER
)
263 self
.SetBackgroundColour(wxWHITE
)
264 self
.SetSize(wxSize(-1, 40))
265 self
.colour
= self
.thickness
= None
266 EVT_PAINT(self
, self
.OnPaint
)
269 def Update(self
, colour
, thickness
):
271 The doodle window calls this method any time the colour
272 or line thickness changes.
275 self
.thickness
= thickness
276 self
.Refresh() # generate a paint event
279 def OnPaint(self
, event
):
281 This method is called when all or part of the window needs to be
286 sz
= self
.GetClientSize()
287 pen
= wxPen(wxNamedColour(self
.colour
), self
.thickness
)
290 dc
.DrawLine(10, sz
.height
/2, sz
.width
-10, sz
.height
/2)
294 #----------------------------------------------------------------------
296 class DoodleAbout(wxDialog
):
297 """ An about box that uses an HTML window """
301 <body bgcolor="#ACAA60">
302 <center><table bgcolor="#455481" width="100%" cellspacing="0"
303 cellpadding="0" border="1">
305 <td align="center"><h1>SuperDoodle</h1></td>
309 <p><b>SuperDoodle</b> is a demonstration program for <b>wxPython</b> that
310 will hopefully teach you a thing or two. Just follow these simple
314 <li><b>Read</b> the Source...
319 <p><b>SuperDoodle</b> and <b>wxPython</b> are brought to you by
320 <b>Robin Dunn</b> and <b>Total Control Software</b>, Copyright
321 © 1997-2001.</p>
326 def __init__(self
, parent
):
327 wxDialog
.__init
__(self
, parent
, -1, 'About SuperDoodle',
328 size
=wxSize(420, 380))
329 from wxPython
.html
import wxHtmlWindow
331 html
= wxHtmlWindow(self
, -1)
332 html
.SetPage(self
.text
)
333 button
= wxButton(self
, wxID_OK
, "Okay")
335 # constraints for the html window
336 lc
= wxLayoutConstraints()
337 lc
.top
.SameAs(self
, wxTop
, 5)
338 lc
.left
.SameAs(self
, wxLeft
, 5)
339 lc
.bottom
.SameAs(button
, wxTop
, 5)
340 lc
.right
.SameAs(self
, wxRight
, 5)
341 html
.SetConstraints(lc
)
343 # constraints for the button
344 lc
= wxLayoutConstraints()
345 lc
.bottom
.SameAs(self
, wxBottom
, 5)
346 lc
.centreX
.SameAs(self
, wxCentreX
)
349 button
.SetConstraints(lc
)
351 self
.SetAutoLayout(true
)
353 self
.CentreOnParent(wxBOTH
)
356 #----------------------------------------------------------------------
358 class DoodleApp(wxApp
):
360 frame
= DoodleFrame(None)
362 self
.SetTopWindow(frame
)
366 #----------------------------------------------------------------------
368 if __name__
== '__main__':