wxpython动态实时绘图_wxPython: 简单的绘图例子
#!/usr/bin/env python#-*- coding: utf-8 -*-
'''Function:繪圖
Input:NONE
Output: NONE
author: socrates
blog:http://www.cnblogs.com/dyx1024/
date:2012-07-11'''
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)if __name__ == '__main__':
app=wx.PySimpleApp()
frame=PaintFrame(None)
frame.Show(True)
app.MainLoop()
總結
以上是生活随笔為你收集整理的wxpython动态实时绘图_wxPython: 简单的绘图例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 昆明昆陆医院脑科在线咨询(昆明昆陆医院)
- 下一篇: tp 查询3天内是否有记录_DNF:玩家