wxPython实现画图板
本文实例为大家分享了wxPython画图板展示的具体代码,供大家参考,具体内容如下
#coding:GBK ''' Createdon2012-3-22 @author:cWX53049 ''' importwx importtime frombsddb.test.test_pickleimportcPickle importos _formatStr="%Y-%m-%d%H:%M:%S" classSkethWindow(wx.Window): def__init__(self,parent,ID): wx.Window.__init__(self,parent,ID) self.SetBackgroundColour("White") self.color="Black" self.thickness=1 self.pen=wx.Pen(self.color,self.thickness,wx.SOLID)#创建一个pen 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_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=False defGetLinesData(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): ifevent.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=newPos defOnSize(self,event): self.reInitBuffer=True#处理一个resize事件 defOnIdle(self,event): ifself.reInitBuffer: self.InitBuffer() self.Refresh() defOnPaint(self,event): wx.BufferedPaintDC(self,self.buffer) defDrawLines(self,dc): forcolur,thickness,lineinself.lines: pen=wx.Pen(colur,thickness,wx.SOLID) dc.SetPen(pen) forcoordsinline: 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) classSkethFrame(wx.Frame): def__init__(self,parent): self.title="绘画窗体" wx.Frame.__init__(self,parent,-1,self.title,size=(500,500)) self.sketh=SkethWindow(self,-1) self.InitStatusBar() self.sketh.Bind(wx.EVT_MOTION,self.showPosition) self.initMenuBar() self.wildcard="Sketchfile(*.sketch)|Allfiles(*.*)|*.*" self.fileName="" defOnNew(self,event): pass defOnOpen(self,event): pass defOnSave(self,event): ifnotself.fileName: self.OnSaveAs(event) else: self.SaveFile() defSaveFile(self): ifself.fileName: data=self.sketh.GetLinesData() f=open(self.fileName,'w') cPickle.dump(data,f) f.close() defOnSaveAs(self,event): dlf=wx.FileDialog(self,"Sketchfile",os.getcwd(),style=wx.SAVE|wx.OVERWRITE_PROMPT) ifdlf.ShowModal()==wx.ID_OK: fileName=dlf.GetPath() ifnotos.path.splitext(fileName)[1]: fileName=fileName+'.sketch' self.fileName=fileName self.SaveFile() self.SetTitle(self.title+'--'+self.fileName) dlf.Destroy() defOnColor(self,event): pass defOnCloseWindow(self,event): self.Destroy() defmenuData(self): return[("&File",( ("&New","NewSketchfile",self.OnNew), ("&Open","opensketchfile",self.OnOpen), ("&Save","savesktechfile",self.OnSave), ("","",""), ("&Color",( ("&Black","",self.OnColor,wx.ITEM_RADIO), ("&Red","",self.OnColor,wx.ITEM_RADIO), ("&Red","",self.OnColor,wx.ITEM_RADIO), ("&Blue","",self.OnColor,wx.ITEM_RADIO))), ("","",""), ("&Quit","quitsketch",self.OnCloseWindow)))] definitMenuBar(self): menuBar=wx.MenuBar() foreachMenuinself.menuData(): eachMenuLabel=eachMenu[0] eachMenuItems=eachMenu[1] menuBar.Append(self.createMenu(eachMenuItems),eachMenuLabel) self.SetMenuBar(menuBar) defcreateMenu(self,menuData): menu=wx.Menu() foreachMenuinmenuData: iflen(eachMenu)==2: label=eachMenu[0] subBar=self.createMenu(eachMenu[1]) menu.AppendMenu(wx.NewId(),label,subBar) else: self.createMenuItem(menu,*eachMenu) returnmenu defcreateMenuItem(self,menu,label,status,handler,kind=wx.ITEM_NORMAL): ifnotlabel: menu.AppendSeparator() return menuItem=menu.Append(-1,label,status,kind) self.Bind(wx.EVT_MENU,handler,menuItem) defInitStatusBar(self): self.statusBar=self.CreateStatusBar() self.statusBar.SetFieldsCount(4) self.statusBar.SetStatusWidths([-1,-2,-2,-2]) self.timer=wx.Timer(self) self.Bind(wx.EVT_TIMER,self.updateTime,self.timer) self.timer.Start(1000) self.statusBar.SetStatusText("Timeis%s"%time.strftime(_formatStr,time.localtime()),3) self.statusBar.SetStatusText("Pos(0,0)",1) self.statusBar.SetStatusText("CurLine0",2) self.statusBar.SetStatusText("Lines0",3) defshowPosition(self,event): self.statusBar.SetStatusText("Pos%s"%str(event.GetPositionTuple()),0) self.statusBar.SetStatusText("CurLine%s"%len(self.sketh.curLine),1) self.statusBar.SetStatusText("Lines%s"%len(self.sketh.lines),2) event.Skip() defupdateTime(self,event): self.statusBar.SetStatusText("Timeis%s"%time.strftime(_formatStr,time.localtime()),3) if__name__=="__main__": app=wx.PySimpleApp(False) frame=SkethFrame(None) frame.Center() frame.Show(True) app.MainLoop()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。