]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-06/example5.py
2 from example1
import SketchWindow
5 class SketchFrame(wx
.Frame
):
6 def __init__(self
, parent
):
7 wx
.Frame
.__init
__(self
, parent
, -1, "Sketch Frame",
9 self
.sketch
= SketchWindow(self
, -1)
10 self
.sketch
.Bind(wx
.EVT_MOTION
, self
.OnSketchMotion
)
15 def initStatusBar(self
):
16 self
.statusbar
= self
.CreateStatusBar()
17 self
.statusbar
.SetFieldsCount(3)
18 self
.statusbar
.SetStatusWidths([-1, -2, -3])
20 def OnSketchMotion(self
, event
):
21 self
.statusbar
.SetStatusText("Pos: %s" %
22 str(event
.GetPositionTuple()), 0)
23 self
.statusbar
.SetStatusText("Current Pts: %s" %
24 len(self
.sketch
.curLine
), 1)
25 self
.statusbar
.SetStatusText("Line Count: %s" %
26 len(self
.sketch
.lines
), 2)
31 ("&New", "New Sketch file", self
.OnNew
),
32 ("&Open", "Open sketch file", self
.OnOpen
),
33 ("&Save", "Save sketch file", self
.OnSave
),
36 ("&Black", "", self
.OnColor
, wx
.ITEM_RADIO
),
37 ("&Red", "", self
.OnColor
, wx
.ITEM_RADIO
),
38 ("&Green", "", self
.OnColor
, wx
.ITEM_RADIO
),
39 ("&Blue", "", self
.OnColor
, wx
.ITEM_RADIO
))),
41 ("&Quit", "Quit", self
.OnCloseWindow
)))]
43 def createMenuBar(self
):
44 menuBar
= wx
.MenuBar()
45 for eachMenuData
in self
.menuData():
46 menuLabel
= eachMenuData
[0]
47 menuItems
= eachMenuData
[1]
48 menuBar
.Append(self
.createMenu(menuItems
), menuLabel
)
49 self
.SetMenuBar(menuBar
)
51 def createMenu(self
, menuData
):
53 for eachItem
in menuData
:
54 if len(eachItem
) == 2:
56 subMenu
= self
.createMenu(eachItem
[1])
57 menu
.AppendMenu(wx
.NewId(), label
, subMenu
)
59 self
.createMenuItem(menu
, *eachItem
)
62 def createMenuItem(self
, menu
, label
, status
, handler
, kind
=wx
.ITEM_NORMAL
):
64 menu
.AppendSeparator()
66 menuItem
= menu
.Append(-1, label
, status
, kind
)
67 self
.Bind(wx
.EVT_MENU
, handler
, menuItem
)
69 def createToolBar(self
):
70 toolbar
= self
.CreateToolBar()
71 for each
in self
.toolbarData():
72 self
.createSimpleTool(toolbar
, *each
)
73 toolbar
.AddSeparator()
74 for each
in self
.toolbarColorData():
75 self
.createColorTool(toolbar
, each
)
78 def createSimpleTool(self
, toolbar
, label
, filename
, help, handler
):
80 toolbar
.AddSeparator()
82 bmp
= wx
.Image(filename
, wx
.BITMAP_TYPE_BMP
).ConvertToBitmap()
83 tool
= toolbar
.AddSimpleTool(-1, bmp
, label
, help)
84 self
.Bind(wx
.EVT_MENU
, handler
, tool
)
86 def toolbarData(self
):
87 return (("New", "new.bmp", "Create new sketch", self
.OnNew
),
89 ("Open", "open.bmp", "Open existing sketch", self
.OnOpen
),
90 ("Save", "save.bmp", "Save existing sketch", self
.OnSave
))
92 def createColorTool(self
, toolbar
, color
):
93 bmp
= self
.MakeBitmap(color
)
94 tool
= toolbar
.AddRadioTool(-1, bmp
, shortHelp
=color
)
95 self
.Bind(wx
.EVT_MENU
, self
.OnColor
, tool
)
97 def MakeBitmap(self
, color
):
98 bmp
= wx
.EmptyBitmap(16, 15)
101 dc
.SetBackground(wx
.Brush(color
))
103 dc
.SelectObject(wx
.NullBitmap
)
106 def toolbarColorData(self
):
107 return ("Black", "Red", "Green", "Blue")
109 def OnNew(self
, event
): pass
110 def OnOpen(self
, event
): pass
111 def OnSave(self
, event
): pass
113 def OnColor(self
, event
):
114 menubar
= self
.GetMenuBar()
115 itemId
= event
.GetId()
116 item
= menubar
.FindItemById(itemId
)
118 toolbar
= self
.GetToolBar()
119 item
= toolbar
.FindById(itemId
)
120 color
= item
.GetShortHelp()
122 color
= item
.GetLabel()
123 self
.sketch
.SetColor(color
)
125 def OnCloseWindow(self
, event
):
128 if __name__
== '__main__':
129 app
= wx
.PySimpleApp()
130 frame
= SketchFrame(None)