wxPython实现绘图小例子

yipeiwu_com5年前Python基础

本文实例为大家分享了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
''' 
 
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()

测试:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python访问mysql数据库的实现方法(2则示例)

本文实例讲述了python访问mysql数据库的实现方法。分享给大家供大家参考,具体如下: 首先安装与Python版本匹配的MySQLdb 示例一 import MySQLdb co...

python requests 使用快速入门

快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引。其假设你已经安装了 Requests。如果还没有,去安装一节看看吧。 首先,确认一下: Requests...

python用win32gui遍历窗口并设置窗口位置的方法

最近电脑打开某个软件却看不见窗口,在任务栏上看到软件明明已经运行,猜想一定是什么原因造成软件窗口位置偏离屏幕的有效坐标太远。尝试重启电脑,重装软件,都没有解决,看来是在注册表存储了位置信...

python 脚本生成随机 字母 + 数字密码功能

下面一段代码给大家介绍python 脚本生成随机 字母 + 数字密码功能,具体代码如下所述: #coding:utf-8 import random,string def GetPa...

基于python实现在excel中读取与生成随机数写入excel中

基于python实现在excel中读取与生成随机数写入excel中

具体要求是:在一份已知的excel表格中读取学生的学号与姓名,再将这些数据放到新的excel表中的第一列与第二列,最后再生成随机数作为学生的考试成绩。 首先要用到的数据库有:xlwt,x...