wxPython实现画图板


Posted in Python onAugust 27, 2020

本文实例为大家分享了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)#创建一个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()#得到鼠标的位置
  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#处理一个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实现画图板

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python threading多线程编程实例
Sep 18 Python
Python 操作MySQL详解及实例
Apr 30 Python
windows下python连接oracle数据库
Jun 07 Python
基于python实现在excel中读取与生成随机数写入excel中
Jan 04 Python
Python如何抓取天猫商品详细信息及交易记录
Feb 23 Python
Python学习小技巧总结
Jun 10 Python
Python3随机漫步生成数据并绘制
Aug 27 Python
python画柱状图--不同颜色并显示数值的方法
Dec 13 Python
Python进阶:生成器 懒人版本的迭代器详解
Jun 29 Python
在 Pycharm 安装使用black的方法详解
Apr 02 Python
Python自带的IDE在哪里
Jul 01 Python
如何用Python进行时间序列分解和预测
Mar 01 Python
如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱
Nov 19 #Python
Python高级特性之闭包与装饰器实例详解
Nov 19 #Python
Python高级编程之继承问题详解(super与mro)
Nov 19 #Python
Python3 Tkinkter + SQLite实现登录和注册界面
Nov 19 #Python
Python csv文件的读写操作实例详解
Nov 19 #Python
Python高级property属性用法实例分析
Nov 19 #Python
wxPython之wx.DC绘制形状
Nov 19 #Python
You might like
关于PHP二进制流 逐bit的低位在前算法(详解)
2013/06/13 PHP
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
2014/05/07 PHP
Yii入门教程之Yii安装及hello world
2014/11/25 PHP
24条货真价实的PHP代码优化技巧
2016/07/28 PHP
yii通过小物件生成view的方法
2016/10/08 PHP
PHP操作Redis常用技巧总结
2018/04/24 PHP
Alliance vs Liquid BO3 第二场2.13
2021/03/10 DOTA
一个JavaScript的求爱小特效
2014/05/09 Javascript
深入理解javascript构造函数和原型对象
2014/09/23 Javascript
node-webkit打包成exe文件被360误报木马的解决方法
2015/03/11 Javascript
javascript实现全局匹配并替换的方法
2015/04/27 Javascript
javascript实现简单的分页特效
2015/08/12 Javascript
AngularJS中使用HTML5手机摄像头拍照
2016/02/22 Javascript
js实现可键盘控制的简单抽奖程序
2016/07/13 Javascript
Javascript 函数的四种调用模式
2016/11/05 Javascript
微信小程序下拉框功能的实例代码
2018/11/06 Javascript
python实现挑选出来100以内的质数
2015/03/24 Python
python获取指定路径下所有指定后缀文件的方法
2015/05/26 Python
python实现随机森林random forest的原理及方法
2017/12/21 Python
Django 内置权限扩展案例详解
2019/03/04 Python
python面试题之列表声明实例分析
2019/07/08 Python
python字符串查找函数的用法详解
2019/07/08 Python
Python socket模块ftp传输文件过程解析
2019/11/05 Python
python数据分析工具之 matplotlib详解
2020/04/09 Python
python自动化测试三部曲之request+django实现接口测试
2020/10/07 Python
python实现定时发送邮件
2020/12/23 Python
浅谈HTML5 & CSS3的新交互特性
2016/07/19 HTML / CSS
html5录音功能实战示例
2019/03/25 HTML / CSS
应聘教师自荐信
2013/10/12 职场文书
违反工作纪律检讨书
2014/02/15 职场文书
文明演讲稿范文
2014/05/12 职场文书
学校节能减排倡议书
2014/05/16 职场文书
法律专业大学生职业生涯规划书:向目标一步步迈进
2014/09/22 职场文书
校园音乐节目广播稿
2015/08/19 职场文书
聘任合同书
2015/09/21 职场文书
建立共青团委员会的请示
2019/04/02 职场文书