小编给大家分享一下wxPython如何实现绘图小例子,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联公司是专业的阜康网站建设公司,阜康接单;提供成都网站制作、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行阜康网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!一个绘图的例子:
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Function:绘图 Input:NONE Output: NONE author: socrates blog:http://www.cnblogs.com/dyx1024/ date:2012-07-11 ''' import wx class PaintWindow(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) 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() self.CaptureMouse() def OnLeftUp(self, event): if self.HasCapture(): self.lines.append((self.color, self.thickness, self.curLine)) self.curLine = [] self.ReleaseMouse() def OnMotion(self, event): if event.Dragging() and event.LeftIsDown(): dc = wx.BufferedDC(wx.ClientDC(self), self.buffer) 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 def OnIdle(self, event): if self.reInitBuffer: self.InitBuffer() self.Refresh(False) def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.buffer) def DrawLines(self, dc): for colour, thickness, line in self.lines: pen = wx.Pen(colour, 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 PaintFrame(wx.Frame): def __init__(self, parent): wx.Frame.__init__(self, parent, -1, "Panit Frame", size = (800, 600)) self.paint = PaintWindow(self, -1) if __name__ == '__main__': app = wx.PySimpleApp() frame = PaintFrame(None) frame.Show(True) app.MainLoop()
测试:
以上是“wxPython如何实现绘图小例子”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。