wxPython之wx.DC绘制形状-创新互联

本文实例为大家分享了wxPython绘制形状的具体代码,供大家参考,具体内容如下

创新互联建站是一家网站制作、成都网站制作,提供网页设计,网站设计,网站制作,建网站,按需网站策划,网站开发公司,自2013年创立以来是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

绘制形状

除了绘制文本和位图,DC也可以绘制任意的形状和线。这允许我们完全自定义窗口部件和控件的外观。

示例说明

利用PaintDC创建一个简单笑脸控件。

#-*-coding: UTF-8 -*-
#------------------------------------------------------
#Purpose: nothing....

#Author: 阿Bin先生
#Created: 2017年5月21日
#------------------------------------------------------
import wx

class Smiley(wx.PyControl):
  def __init__(self, parent, size=(100, 100)):
    super(Smiley, self).__init__(parent,
    size=size,
    style=wx.NO_BORDER)
    # Event Handlers
    self.Bind(wx.EVT_PAINT, self.OnPaint)

  def OnPaint(self, event):
    """Draw the image on to the panel"""
    dc = wx.PaintDC(self) # Must create a PaintDC
    # Get the working rectangle we can draw in
    rect = self.GetClientRect()
    # Setup the DC
    dc.SetPen(wx.BLACK_PEN) # for drawing lines / borders
    yellowbrush = wx.Brush(wx.Colour(255, 255, 0))
    dc.SetBrush(yellowbrush) # Yellow fill

    cx = (rect.width / 2) + rect.x
    cy = (rect.width / 2) + rect.y
    radius = min(rect.width, rect.height) / 2
    dc.DrawCircle(cx, cy, radius)
    eyesz = (rect.width / 8, rect.height / 8)
    eyepos = (cx / 2, cy / 2)
    dc.SetBrush(wx.BLUE_BRUSH)
    dc.DrawRectangle(eyepos[0], eyepos[1],
    eyesz[0], eyesz[1])
    eyepos = (eyepos[0] + (cx - eyesz[0]), eyepos[1])
    dc.DrawRectangle(eyepos[0], eyepos[1],
    eyesz[0], eyesz[1])
    dc.SetBrush(yellowbrush)
    startpos = (cx / 2, (cy / 2) + cy)
    endpos = (cx + startpos[0], startpos[1])
    dc.DrawArc(startpos[0], startpos[1],
    endpos[0], endpos[1], cx, cy)
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.DrawRectangle(startpos[0], cy,
    endpos[0] - startpos[0],
    startpos[1] - cy)

class MyFrame(wx.Frame):
  def __init__(self, parent, *args, **kwargs):
    super(MyFrame, self).__init__(parent, *args, **kwargs)
    # Attributes
    self.Panel = wx.Panel(self)
    Smiley(self.Panel)

class MyApp(wx.App):
  def OnInit(self):
    self.frame = MyFrame(None, title="DrawShapes",size = [500, 500])
    self.SetTopWindow(self.frame)
    self.frame.Show()
    return True

if __name__ == "__main__":
  app = MyApp(False)
  app.MainLoop()

当前文章:wxPython之wx.DC绘制形状-创新互联
URL分享:http://pwwzsj.com/article/icjgj.html