您好,登錄后才能下訂單哦!
小編給大家分享一下wxPython如何實(shí)現(xiàn)畫圖板,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
wxPython畫圖板展示的具體代碼如下
#coding:GBK ''' Created on 2012-3-22 @author: cWX53049 ''' import wx import time from bsddb.test.test_pickle import cPickle import os _formatStr="%Y-%m-%d %H:%M:%S" class SkethWindow(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)#創(chuàng)建一個(gè)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) def InitBuffer(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 def GetLinesData(self): return self.lines[:] def SetLinesData(self,lines): self.lines = lines[:] self.InitBuffer() self.Refresh() def OnLeftDown(self,event): self.curLine=[] self.pos=event.GetPositionTuple()#得到鼠標(biāo)的位置 self.CaptureMouse()#捕獲鼠標(biāo) def OnLeftUp(self,event): if self.HasCapture(): self.lines.append((self.color, self.thickness, self.curLine)) self.curLine=[] self.ReleaseMouse()#釋放鼠標(biāo) def OnMotion(self,event): if event.Dragging() and event.LeftIsDown():#確定是否在拖動(dòng) dc = wx.BufferedDC(wx.ClientDC(self),self.buffer)#創(chuàng)建另一個(gè) self.drawMotion(dc,event) event.Skip() def drawMotion(self,dc,event): dc.SetPen(self.pen) newPos=event.GetPositionTuple() coords=self.pos+newPos self.curLine.append(coords) dc.DrawLine(*coords) self.pos=newPos def OnSize(self,event): self.reInitBuffer=True#處理一個(gè)resize事件 def OnIdle(self,event): if self.reInitBuffer: self.InitBuffer() self.Refresh() def OnPaint(self,event): wx.BufferedPaintDC(self,self.buffer) def DrawLines(self,dc): for colur,thickness,line in self.lines: pen = wx.Pen(colur,thickness,wx.SOLID) dc.SetPen(pen) for coords in line: dc.DrawLine(*coords) def SetColor(self,color): self.color = color self.pen = wx.Pen(self.color,self.thickness,wx.SOLID) def SetThickness(self,num): self.thickness=num self.pen=wx.Pen(self.color,self.thickness,wx.SOLID) class SkethFrame(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="Sketch file(*.sketch)|All files(*.*)|*.*" self.fileName="" def OnNew(self,event): pass def OnOpen(self,event): pass def OnSave(self,event): if not self.fileName: self.OnSaveAs(event) else: self.SaveFile() def SaveFile(self): if self.fileName: data = self.sketh.GetLinesData() f = open(self.fileName,'w') cPickle.dump(data, f) f.close() def OnSaveAs(self,event): dlf = wx.FileDialog(self,"Sketch file",os.getcwd(),style=wx.SAVE|wx.OVERWRITE_PROMPT) if dlf.ShowModal()==wx.ID_OK: fileName = dlf.GetPath() if not os.path.splitext(fileName)[1]: fileName=fileName+'.sketch' self.fileName=fileName self.SaveFile() self.SetTitle(self.title+'--'+self.fileName) dlf.Destroy() def OnColor(self,event): pass def OnCloseWindow(self,event): self.Destroy() def menuData(self): return [("&File",( ("&New","New Sketch file",self.OnNew), ("&Open","open sketch file",self.OnOpen), ("&Save","save sktech file",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","quit sketch ",self.OnCloseWindow)))] def initMenuBar(self): menuBar=wx.MenuBar() for eachMenu in self.menuData(): eachMenuLabel = eachMenu[0] eachMenuItems = eachMenu[1] menuBar.Append(self.createMenu(eachMenuItems),eachMenuLabel) self.SetMenuBar(menuBar) def createMenu(self,menuData): menu = wx.Menu() for eachMenu in menuData: if len(eachMenu)==2: label = eachMenu[0] subBar =self.createMenu(eachMenu[1]) menu.AppendMenu(wx.NewId(),label,subBar) else: self.createMenuItem(menu,*eachMenu) return menu def createMenuItem(self,menu,label,status,handler,kind=wx.ITEM_NORMAL): if not label: menu.AppendSeparator() return menuItem=menu.Append(-1,label,status,kind) self.Bind(wx.EVT_MENU,handler,menuItem) def InitStatusBar(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("Time is %s"%time.strftime(_formatStr,time.localtime()),3) self.statusBar.SetStatusText("Pos (0,0)",1) self.statusBar.SetStatusText("Cur Line 0",2) self.statusBar.SetStatusText("Lines 0" ,3) def showPosition(self,event): self.statusBar.SetStatusText("Pos %s"%str(event.GetPositionTuple()),0) self.statusBar.SetStatusText("Cur Line %s"%len(self.sketh.curLine),1) self.statusBar.SetStatusText("Lines %s"%len(self.sketh.lines),2) event.Skip() def updateTime(self,event): self.statusBar.SetStatusText("Time is %s"%time.strftime(_formatStr,time.localtime()),3) if __name__=="__main__": app = wx.PySimpleApp(False) frame=SkethFrame(None) frame.Center() frame.Show(True) app.MainLoop()
以上是“wxPython如何實(shí)現(xiàn)畫圖板”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。