这是一个完整的画图板demo ,觉得很不错
更详细的介绍,参见:资源下载中的wxpython教程的第6章
本demo中用到的图片:
open.png:
save.png:
new.png:
splash.png:
源代码:
class SketchAbout(wx.Dialog):
text = '''
<html>
<body bgcolor="#ACAA60">
<center><table bgcolor="#455481" width="100%" cellspacing="0"
cellpadding="0" border="1">
<tr>
<td align="center"><h1>Sketch!</h1></td>
</tr>
</table>
</center>
<p><b>Sketch</b> is a demonstration program for <b>wxPython In Action</b>
Chapter 7. It is based on the SuperDoodle demo included with wxPython,
available at http://www.wxpython.org/
</p>
<p><b>SuperDoodle</b> and <b>wxPython</b> are brought to you by
<b>Robin Dunn</b> and <b>Total Control Software</b>, Copyright
© 1997-2006.</p>
</body>
</html>
'''
def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, 'About Sketch',
size=(440, 400) )
html = wx.html.HtmlWindow(self)
html.SetPage(self.text)
button = wx.Button(self, wx.ID_OK, "Okay")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5)
sizer.Add(button, 0, wx.ALIGN_CENTER|wx.ALL, 5)
self.SetSizer(sizer)
self.Layout()
class ControlPanel(wx.Panel):
BMP_SIZE = 16
BMP_BORDER = 3
NUM_COLS = 4
SPACING = 4
colorList = ('Black', 'Yellow', 'Red', 'Green', 'Blue', 'Purple',
'Brown', 'Aquamarine', 'Forest Green', 'Light Blue',
'Goldenrod', 'Cyan', 'Orange', 'Navy', 'Dark Grey',
'Light Grey')
maxThickness = 16
def __init__(self, parent, ID, sketch):
wx.Panel.__init__(self, parent, ID, style=wx.RAISED_BORDER)
self.sketch = sketch
buttonSize = (self.BMP_SIZE + 2 * self.BMP_BORDER,
self.BMP_SIZE + 2 * self.BMP_BORDER)
colorGrid = self.createColorGrid(parent, buttonSize)
thicknessGrid = self.createThicknessGrid(buttonSize)
self.layout(colorGrid, thicknessGrid)
def createColorGrid(self, parent, buttonSize):
self.colorMap = {}
self.colorButtons = {}
colorGrid = wx.GridSizer(cols=self.NUM_COLS, hgap=2, vgap=2)
for eachColor in self.colorList:
bmp = parent.MakeBitmap(eachColor)
b = buttons.GenBitmapToggleButton(self, -1, bmp, size=buttonSize)
b.SetBezelWidth(1)
b.SetUseFocusIndicator(False)
self.Bind(wx.EVT_BUTTON, self.OnSetColour, b)
colorGrid.Add(b, 0)
self.colorMap[b.GetId()] = eachColor
self.colorButtons[eachColor] = b
self.colorButtons[self.colorList[0]].SetToggle(True)
return colorGrid
def createThicknessGrid(self, buttonSize):
self.thicknessIdMap = {}
self.thicknessButtons = {}
thicknessGrid = wx.GridSizer(cols=self.NUM_COLS, hgap=2, vgap=2)
for x in range(1, self.maxThickness + 1):
b = buttons.GenToggleButton(self, -1, str(x), size=buttonSize)
b.SetBezelWidth(1)
b.SetUseFocusIndicator(False)
self.Bind(wx.EVT_BUTTON, self.OnSetThickness, b)
thicknessGrid.Add(b, 0)
self.thicknessIdMap[b.GetId()] = x
self.thicknessButtons[x] = b
self.thicknessButtons[1].SetToggle(True)
return thicknessGrid
def layout(self, colorGrid, thicknessGrid):
box = wx.BoxSizer(wx.VERTICAL)
box.Add(colorGrid, 0, wx.ALL, self.SPACING)
box.Add(thicknessGrid, 0, wx.ALL, self.SPACING)
self.SetSizer(box)
box.Fit(self)
def OnSetColour(self, event):
color = self.colorMap[event.GetId()]
if color != self.sketch.color and self.sketch.color in self.colorButtons:
self.colorButtons[self.sketch.color].SetToggle(False)
self.sketch.SetColor(color)
def OnSetThickness(self, event):
thickness = self.thicknessIdMap[event.GetId()]
if thickness != self.sketch.thickness:
self.thicknessButtons[self.sketch.thickness].SetToggle(False)
self.sketch.SetThickness(thickness)
class SketchApp(wx.App):
def OnInit(self):
bmp = wx.Image("splash.png").ConvertToBitmap()
wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
1000, None, -1)
wx.Yield()
frame = SketchFrame(None)
frame.Show(True)
self.SetTopWindow(frame)
return True
if __name__ == '__main__':
app = SketchApp(False)
app.MainLoop()