#!/usr/bin/env python#-*- coding: utf-8 -*-
'''Function:绘图
Input:NONE
Output: NONE
author: socrates
blog:http://www.cnblogs.com/dyx1024/
date:2012-07-13'''
importwxclassPaintWindow(wx.Window):def __init__(self, parent, id):
wx.Window.__init__(self, parent, id)
self.SetBackgroundColour("Red")
self.color= "Green"self.thickness= 10
#创建一个画笔
self.pen =wx.Pen(self.color, self.thickness, wx.SOLID)
self.lines=[]
self.curLine=[]
self.pos=(0, 0)
self.InitBuffer()#连接事件
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_PAINT, self.OnPaint)defInitBuffer(self):
size=self.GetClientSize()#创建缓存的设备上下文
self.buffer =wx.EmptyBitmap(size.width, size.height)
dc=wx.BufferedDC(None, self.buffer)#使用设备上下文
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
self.DrawLines(dc)
self.reInitBuffer=FalsedefGetLinesData(self):returnself.lines[:]defSetLinesData(self, lines):
self.lines=lines[:]
self.InitBuffer()
self.Refresh()defOnLeftDown(self, event):
self.curLine=[]#获取鼠标位置
self.pos =event.GetPositionTuple()
self.CaptureMouse()defOnLeftUp(self, event):ifself.HasCapture():
self.lines.append((self.color,
self.thickness,
self.curLine))
self.curLine=[]
self.ReleaseMouse()defOnMotion(self, event):if event.Dragging() andevent.LeftIsDown():
dc=wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.drawMotion(dc, event)
event.Skip()defdrawMotion(self, dc, event):
dc.SetPen(self.pen)
newPos=event.GetPositionTuple()
coords= self.pos +newPos
self.curLine.append(coords)
dc.DrawLine(*coords)
self.pos=newPosdefOnSize(self, event):
self.reInitBuffer=TruedefOnIdle(self, event):ifself.reInitBuffer:
self.InitBuffer()
self.Refresh(False)defOnPaint(self, event):
dc=wx.BufferedPaintDC(self, self.buffer)defDrawLines(self, dc):for colour, thickness, line inself.lines:
pen=wx.Pen(colour, thickness, wx.SOLID)
dc.SetPen(pen)for coords inline:
dc.DrawLine(*coords)defSetColor(self, color):
self.color=color
self.pen=wx.Pen(self.color, self.thickness, wx.SOLID)defSetThickness(self, num):
self.thickness=num
self.pen=wx.Pen(self.color, self.thickness, wx.SOLID)classPaintFrame(wx.Frame):def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Panit Frame", size = (800, 600))
self.paint= PaintWindow(self, -1)#状态栏
self.paint.Bind(wx.EVT_MOTION, self.OnPaintMotion)
self.InitStatusBar()#创建菜单
self.CreateMenuBar()defInitStatusBar(self):
self.statusbar=self.CreateStatusBar()#将状态栏分割为3个区域,比例为1:2:3
self.statusbar.SetFieldsCount(3)
self.statusbar.SetStatusWidths([-1, -2, -3])defOnPaintMotion(self, event):#设置状态栏1内容
self.statusbar.SetStatusText(u"鼠标位置:" +str(event.GetPositionTuple()), 0)#设置状态栏2内容
self.statusbar.SetStatusText(u"当前线条长度:%s" % len(self.paint.curLine), 1)#设置状态栏3内容
self.statusbar.SetStatusText(u"线条数目:%s" % len(self.paint.lines), 2)
event.Skip()defMenuData(self):'''菜单数据'''
#格式:菜单数据的格式现在是(标签, (项目)),其中:项目组成为:标签, 描术文字, 处理器, 可选的kind
#标签长度为2,项目的长度是3或4
return [("&File", ( #一级菜单项
("&New", "New paint file", self.OnNew), #二级菜单项
("&Open", "Open paint file", self.OnOpen),
("&Save", "Save paint file", self.OnSave),
("", "", ""), #分隔线
("&Color", (
("&Black", "", self.OnColor, wx.ITEM_RADIO), #三级菜单项,单选
("&Red", "", self.OnColor, wx.ITEM_RADIO),
("&Green", "", self.OnColor, wx.ITEM_RADIO),
("&Blue", "", self.OnColor, wx.ITEM_RADIO))),
("", "", ""),
("&Quit", "Quit", self.OnCloseWindow)))
]defCreateMenuBar(self):'''创建菜单'''menuBar=wx.MenuBar()for eachMenuData inself.MenuData():
menuLabel=eachMenuData[0]
menuItems= eachMenuData[1]
menuBar.Append(self.CreateMenu(menuItems), menuLabel)
self.SetMenuBar(menuBar)defCreateMenu(self, menuData):'''创建一级菜单'''menu=wx.Menu()for eachItem inmenuData:if len(eachItem) == 2:
label=eachItem[0]
subMenu= self.CreateMenu(eachItem[1])
menu.AppendMenu(wx.NewId(), label, subMenu)#递归创建菜单项
else:
self.CreateMenuItem(menu,*eachItem)returnmenudef CreateMenuItem(self, menu, label, status, handler, kind =wx.ITEM_NORMAL):'''创建菜单项内容'''
if notlabel:
menu.AppendSeparator()returnmenuItem= menu.Append(-1, label, status, kind)
self.Bind(wx.EVT_MENU, handler,menuItem)defOnNew(self, event):pass
defOnOpen(self, event):pass
defOnSave(self, event):pass
defOnColor(self, event):'''更改画笔内容'''menubar=self.GetMenuBar()
itemid=event.GetId()
item=menubar.FindItemById(itemid)
color= item.GetLabel() #获取菜单项内容
self.paint.SetColor(color)defOnCloseWindow(self, event):
self.Destroy()if __name__ == '__main__':
app=wx.PySimpleApp()
frame=PaintFrame(None)
frame.Show(True)
app.MainLoop()