]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-06/example6.py
5 from example1
import SketchWindow
8 class SketchFrame(wx
.Frame
):
9 def __init__(self
, parent
):
10 self
.title
= "Sketch Frame"
11 wx
.Frame
.__init
__(self
, parent
, -1, self
.title
,
14 self
.sketch
= SketchWindow(self
, -1)
15 self
.sketch
.Bind(wx
.EVT_MOTION
, self
.OnSketchMotion
)
20 def initStatusBar(self
):
21 self
.statusbar
= self
.CreateStatusBar()
22 self
.statusbar
.SetFieldsCount(3)
23 self
.statusbar
.SetStatusWidths([-1, -2, -3])
25 def OnSketchMotion(self
, event
):
26 self
.statusbar
.SetStatusText("Pos: %s" %
27 str(event
.GetPositionTuple()), 0)
28 self
.statusbar
.SetStatusText("Current Pts: %s" %
29 len(self
.sketch
.curLine
), 1)
30 self
.statusbar
.SetStatusText("Line Count: %s" %
31 len(self
.sketch
.lines
), 2)
36 ("&New", "New Sketch file", self
.OnNew
),
37 ("&Open", "Open sketch file", self
.OnOpen
),
38 ("&Save", "Save sketch file", self
.OnSave
),
41 ("&Black", "", self
.OnColor
, wx
.ITEM_RADIO
),
42 ("&Red", "", self
.OnColor
, wx
.ITEM_RADIO
),
43 ("&Green", "", self
.OnColor
, wx
.ITEM_RADIO
),
44 ("&Blue", "", self
.OnColor
, wx
.ITEM_RADIO
),
45 ("&Other...", "", self
.OnOtherColor
, wx
.ITEM_RADIO
))),
47 ("&Quit", "Quit", self
.OnCloseWindow
)))]
49 def createMenuBar(self
):
50 menuBar
= wx
.MenuBar()
51 for eachMenuData
in self
.menuData():
52 menuLabel
= eachMenuData
[0]
53 menuItems
= eachMenuData
[1]
54 menuBar
.Append(self
.createMenu(menuItems
), menuLabel
)
55 self
.SetMenuBar(menuBar
)
57 def createMenu(self
, menuData
):
59 for eachItem
in menuData
:
60 if len(eachItem
) == 2:
62 subMenu
= self
.createMenu(eachItem
[1])
63 menu
.AppendMenu(wx
.NewId(), label
, subMenu
)
65 self
.createMenuItem(menu
, *eachItem
)
68 def createMenuItem(self
, menu
, label
, status
, handler
, kind
=wx
.ITEM_NORMAL
):
70 menu
.AppendSeparator()
72 menuItem
= menu
.Append(-1, label
, status
, kind
)
73 self
.Bind(wx
.EVT_MENU
, handler
, menuItem
)
75 def createToolBar(self
):
76 toolbar
= self
.CreateToolBar()
77 for each
in self
.toolbarData():
78 self
.createSimpleTool(toolbar
, *each
)
79 toolbar
.AddSeparator()
80 for each
in self
.toolbarColorData():
81 self
.createColorTool(toolbar
, each
)
84 def createSimpleTool(self
, toolbar
, label
, filename
, help, handler
):
86 toolbar
.AddSeparator()
88 bmp
= wx
.Image(filename
, wx
.BITMAP_TYPE_BMP
).ConvertToBitmap()
89 tool
= toolbar
.AddSimpleTool(-1, bmp
, label
, help)
90 self
.Bind(wx
.EVT_MENU
, handler
, tool
)
92 def toolbarData(self
):
93 return (("New", "new.bmp", "Create new sketch", self
.OnNew
),
95 ("Open", "open.bmp", "Open existing sketch", self
.OnOpen
),
96 ("Save", "save.bmp", "Save existing sketch", self
.OnSave
))
98 def createColorTool(self
, toolbar
, color
):
99 bmp
= self
.MakeBitmap(color
)
100 tool
= toolbar
.AddRadioTool(-1, bmp
, shortHelp
=color
)
101 self
.Bind(wx
.EVT_MENU
, self
.OnColor
, tool
)
103 def MakeBitmap(self
, color
):
104 bmp
= wx
.EmptyBitmap(16, 15)
107 dc
.SetBackground(wx
.Brush(color
))
109 dc
.SelectObject(wx
.NullBitmap
)
112 def toolbarColorData(self
):
113 return ("Black", "Red", "Green", "Blue")
115 def OnNew(self
, event
): pass
117 def OnColor(self
, event
):
118 menubar
= self
.GetMenuBar()
119 itemId
= event
.GetId()
120 item
= menubar
.FindItemById(itemId
)
122 toolbar
= self
.GetToolBar()
123 item
= toolbar
.FindById(itemId
)
124 color
= item
.GetShortHelp()
126 color
= item
.GetLabel()
127 self
.sketch
.SetColor(color
)
129 def OnCloseWindow(self
, event
):
134 data
= self
.sketch
.GetLinesData()
135 f
= open(self
.filename
, 'w')
136 cPickle
.dump(data
, f
)
142 f
= open(self
.filename
, 'r')
143 data
= cPickle
.load(f
)
145 self
.sketch
.SetLinesData(data
)
146 except cPickle
.UnpicklingError
:
147 wx
.MessageBox("%s is not a sketch file." % self
.filename
,
148 "oops!", style
=wx
.OK|wx
.ICON_EXCLAMATION
)
150 wildcard
= "Sketch files (*.sketch)|*.sketch|All files (*.*)|*.*"
152 def OnOpen(self
, event
):
153 dlg
= wx
.FileDialog(self
, "Open sketch file...", os
.getcwd(),
154 style
=wx
.OPEN
, wildcard
=self
.wildcard
)
155 if dlg
.ShowModal() == wx
.ID_OK
:
156 self
.filename
= dlg
.GetPath()
158 self
.SetTitle(self
.title
+ ' -- ' + self
.filename
)
161 def OnSave(self
, event
):
162 if not self
.filename
:
167 def OnSaveAs(self
, event
):
168 dlg
= wx
.FileDialog(self
, "Save sketch as...", os
.getcwd(),
169 style
=wx
.SAVE | wx
.OVERWRITE_PROMPT
,
170 wildcard
= self
.wildcard
)
171 if dlg
.ShowModal() == wx
.ID_OK
:
172 filename
= dlg
.GetPath()
173 if not os
.path
.splitext(filename
)[1]:
174 filename
= filename
+ '.sketch'
175 self
.filename
= filename
177 self
.SetTitle(self
.title
+ ' -- ' + self
.filename
)
180 def OnOtherColor(self
, event
):
181 dlg
= wx
.ColourDialog(frame
)
182 dlg
.GetColourData().SetChooseFull(True)
183 if dlg
.ShowModal() == wx
.ID_OK
:
184 self
.sketch
.SetColor(dlg
.GetColourData().GetColour())
188 if __name__
== '__main__':
189 app
= wx
.PySimpleApp()
190 frame
= SketchFrame(None)