wxPython實(shí)現(xiàn)繪圖小例子
本文實(shí)例為大家分享了wxPython繪圖小例子的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
一個(gè)繪圖的例子:
#!/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
#創(chuàng)建一個(gè)畫(huà)筆
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()
#創(chuàng)建緩存的設(shè)備上下文
self.buffer = wx.EmptyBitmap(size.width, size.height)
dc = wx.BufferedDC(None, self.buffer)
#使用設(shè)備上下文
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 = []
#獲取鼠標(biāo)位置
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()
測(cè)試:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MNIST數(shù)據(jù)集轉(zhuǎn)化為二維圖片的實(shí)現(xiàn)示例
這篇文章主要介紹了MNIST數(shù)據(jù)集轉(zhuǎn)化為二維圖片的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python的Flask框架開(kāi)發(fā)驗(yàn)證碼登錄的實(shí)現(xiàn)
在本文我們介紹了如何使用Python的Flask框架開(kāi)發(fā)一個(gè)簡(jiǎn)單的驗(yàn)證碼登錄功能,將涵蓋生成驗(yàn)證碼、處理用戶(hù)輸入、驗(yàn)證驗(yàn)證碼以及實(shí)現(xiàn)安全的用戶(hù)認(rèn)證等方面,感興趣的可以了解一下2023-11-11
通過(guò)CartPole游戲詳解PPO?優(yōu)化過(guò)程
這篇文章主要為大家介紹了通過(guò)CartPole游戲詳解PPO?優(yōu)化過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python實(shí)現(xiàn)最長(zhǎng)公共子序列
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)最長(zhǎng)公共子序列的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過(guò)公共鍵對(duì)字典列表排序算法示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過(guò)公共鍵對(duì)字典列表排序算法,結(jié)合實(shí)例形式分析了Python基于operator模塊中的itemgetter()函數(shù)對(duì)字典進(jìn)行排序的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
深入了解Python數(shù)據(jù)類(lèi)型之列表
下面小編就為大家?guī)?lái)一篇深入了解Python數(shù)據(jù)類(lèi)型之列表。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Python排序算法快速排序VS歸并排序深入對(duì)比分析
快速排序和歸并排序是兩種常見(jiàn)的排序算法,在Python中有著重要的應(yīng)用,本文將深入探討這兩種算法的原理和實(shí)現(xiàn),并提供豐富的示例代碼來(lái)說(shuō)明它們的工作方式2024-01-01

