Wxpython-GDI (2)

Source: Internet
Author: User
Tags hypot
Document directory
  • Basic primitives
Basic primitivespoint

The simplest geometric object is a point. It is a point on the top of a window.

DrawPoint(int x, int y)
#!/usr/bin/python# -*- coding: utf-8 -*-import wximport randomclass Points(wx.Frame):    def __init__(self, parent, id = -1, title = 'points club'):        wx.Frame.__init__(self, parent, id, title, size=(250, 150))        self.Bind(wx.EVT_PAINT, self.OnPaint)        self.Centre()        self.Show(True)    def OnPaint(self, event):        dc = wx.PaintDC(self)        dc.SetPen(wx.Pen('RED'))        for i in range(1000):            w, h = self.GetSize()            x = random.randint(1, w-1)            y = random.randint(1, h-1)            dc.DrawPoint(x, y)                       if __name__ == '__main__':    app = wx.App()    Points(None)    app.MainLoop()
Shapes

Shapes are more complex geometric objects. We will plot various geometric shapes in the following example.

#!/usr/bin/python# -*- coding: utf-8 -*-import wxclass Shapes(wx.Frame):    def __init__(self, parent, id = -1, title = 'Shapes'):        wx.Frame.__init__(self, parent, id, title, size=(350, 300))        self.Bind(wx.EVT_PAINT, self.OnPaint)        self.Centre()        self.Show(True)    def OnPaint(self, event):        dc = wx.PaintDC(self)        dc.DrawEllipse(20, 20, 90, 60)        dc.DrawRoundedRectangle(130, 20, 90, 60, 10)        dc.DrawArc(240, 40, 340, 40, 290, 20)        dc.DrawPolygon(((130, 140), (180, 170), (180, 140), (220, 110), (140, 100)))        dc.DrawRectangle(20, 120, 80, 50)        dc.DrawSpline(((240, 170), (280, 170), (285, 110), (325, 110)))        dc.DrawLines(((20, 260), (100, 260), (20, 210), (100, 210)))        dc.DrawCircle(170, 230, 35)        dc.DrawRectangle(250, 200, 60, 60)               if __name__ == '__main__':    app = wx.App()    Shapes(None)    app.MainLoop()

 

Regions

The device context can be divided into several parts, called zones. The area can be any shape. The area can be a simple rectangle or circle. With merge, intersection, subtraction and XOR operations, we can simply create complex areas. Area is used for outline display, filling, or editing.

You can create a region in three ways. The simplest way is to create a rectangular area. You can create more complex regions from the bitmap vertex list.

wx.Region(int x=0, int y=0, int width=0, int height=0)
wx.RegionFromPoints(list points, int fillStyle=wx.WINDING_RULE)

 

This constructor creates a polygon area. The fillstyle parameter can be wx. winding_rule or WX. oddeven_rule.

You can use the following classes to create more complex areas.

Wx. regionfrombitmap (wx. bitmap BMP)

Before learning, we will first create a small example. We divide theme into several parts to make it easier to understand. You may find that it will make good use of the mathematics you learned at school.
import wxfrom math import hypot, sin, cos, piclass Example(wx.Frame):    def __init__(self, *args, **kw):        super(Example, self).__init__(*args, **kw)         self.Bind(wx.EVT_PAINT, self.OnPaint)        self.SetSize((350, 250))        self.SetTitle('Lines')        self.Centre()        self.Show(True)    def OnPaint(self, event):              dc = wx.PaintDC(self)        size_x, size_y = self.GetClientSizeTuple()        dc.SetDeviceOrigin(size_x/2, size_y/2)        radius = hypot(size_x/2, size_y/2)        angle = 0        while (angle < 2*pi):            x = radius*cos(angle)            y = radius*sin(angle)            dc.DrawLinePoint((0, 0), (x, y))            angle = angle + 2*pi/360        if __name__ == '__main__':    app = wx.App()    Example(None, -1, 'xxx')    app.MainLoop()

 

In the following example, we draw 260 lines from the center of the customer's region to the periphery. Line deviation.

Method setdeviceorigin () to create a new starting coordinate system. We place it in the middle of the client region. By relocating the coordinate system, we made our drawing less complex.

 

Clipping trim is used to restrict plotting in a specific area. Editing is used in two cases. You need to create results to improve application performance. We use the setclippingregionasregion () method to limit the plot to a certain area.

In the following example, we will modify and enhance our previous example.

 

#!/usr/bin/python# -*- coding: utf-8 -*-import wxfrom math import hypot, sin, cos, piclass Example(wx.Frame):    def __init__(self, parent, id, title):        wx.Frame.__init__(self, parent, id, title, size=(350, 300))        self.Bind(wx.EVT_PAINT, self.OnPaint)        self.Centre()        self.Show(True)    def OnPaint(self, event):        dc = wx.PaintDC(self)        dc.SetPen(wx.Pen('#424242'))        size_x, size_y = self.GetClientSizeTuple()        dc.SetDeviceOrigin(size_x/2, size_y/2)        points = (((0, 85), (75, 75), (100, 10), (125, 75), (200, 85),            (150, 125), (160, 190), (100, 150), (40, 190), (50, 125)))        region = wx.RegionFromPoints(points)        dc.SetClippingRegionAsRegion(region)        radius = hypot(size_x/2, size_y/2)        angle = 0        while (angle < 2*pi):            x = radius*cos(angle)            y = radius*sin(angle)            dc.DrawLinePoint((0, 0), (x, y))            angle = angle + 2*pi/360        dc.DestroyClippingRegion()        if __name__ == '__main__':    app = wx.App()    Example(None, -1, 'xxx')    app.MainLoop()

 

We still drew 360 lines, but this time we drew them in a specific area. This specific region is the regin region generated using the points set.

region = wx.RegionFromPoints(points)dc.SetClippingRegionAsRegion(region)

 

We useWx. regionfrompoins () and points set to create a region.Setclippingregionasregion () method to set the painting area.
dc.DestroyClippingRegion()

 

After clipregion is used up, We must destory it.
 

Operations over regions

Regions can be combined into more complex shapes. We can use four types of operations. Merge, intersection, and subtract XOR operations.

The following example demonstrates these four operations.

#!/usr/bin/python# -*- coding: utf-8 -*-import wxfrom math import hypot, sin, cos, piclass Example(wx.Frame):     def __init__(self, parent, id, title):         wx.Frame.__init__(self, parent, id, title, size=(270, 220))         self.Bind(wx.EVT_PAINT, self.OnPaint)         self.Centre()         self.Show(True)     def OnPaint(self, event):         dc = wx.PaintDC(self)         dc.SetPen(wx.Pen('#d4d4d4'))                  #First         dc.DrawRectangle(20, 20, 50, 50)         dc.DrawRectangle(30, 40, 50, 50)         #Second         dc.SetBrush(wx.Brush('#ffffff'))         dc.DrawRectangle(100, 20, 50, 50)         dc.DrawRectangle(110, 40, 50, 50)          region1 = wx.Region(100, 20, 50, 50)         region2 = wx.Region(110, 40, 50, 50)         region1.IntersectRegion(region2)         rect = region1.GetBox()         dc.SetClippingRegionAsRegion(region1)         dc.SetBrush(wx.Brush('#ff0000'))         dc.DrawRectangleRect(rect)         dc.DestroyClippingRegion()         # 3         dc.SetBrush(wx.Brush('#ffffff'))         dc.DrawRectangle(180, 20, 50, 50)         dc.DrawRectangle(190, 40, 50, 50)         region1 = wx.Region(180, 20, 50, 50)         region2 = wx.Region(190, 40, 50, 50)         region1.UnionRegion(region2)         dc.SetClippingRegionAsRegion(region1)         rect = region1.GetBox()         dc.SetBrush(wx.Brush('#fa8e00'))         dc.DrawRectangleRect(rect)         dc.DestroyClippingRegion()                  #4         dc.SetBrush(wx.Brush('#ffffff'))         dc.DrawRectangle(20, 120, 50, 50)         dc.DrawRectangle(30, 140, 50, 50)         region1 = wx.Region(20, 120, 50, 50)         region2 = wx.Region(30, 140, 50, 50)         region1.XorRegion(region2)         rect = region1.GetBox()         dc.SetClippingRegionAsRegion(region1)         dc.SetBrush(wx.Brush('#619e1b'))         dc.DrawRectangleRect(rect)         dc.DestroyClippingRegion()                  #5         dc.SetBrush(wx.Brush('#ffffff'))         dc.DrawRectangle(100, 120, 50, 50)         dc.DrawRectangle(110, 140, 50, 50)         region1 = wx.Region(100, 120, 50, 50)         region2 = wx.Region(110, 140, 50, 50)         region1.SubtractRegion(region2)         rect = region1.GetBox()         dc.SetClippingRegionAsRegion(region1)         dc.SetBrush(wx.Brush('#715b33'))         dc.DrawRectangleRect(rect)         dc.DestroyClippingRegion()         #6         dc.SetBrush(wx.Brush('#ffffff'))         dc.DrawRectangle(180, 120, 50, 50)         dc.DrawRectangle(190, 140, 50, 50)         region1 = wx.Region(180, 120, 50, 50)         region2 = wx.Region(190, 140, 50, 50)         region2.SubtractRegion(region1)         rect = region2.GetBox()         dc.SetClippingRegionAsRegion(region2)         dc.SetBrush(wx.Brush('#0d0060'))         dc.DrawRectangleRect(rect)         dc.DestroyClippingRegion()        if __name__ == '__main__':    app = wx.App()    Example(None, -1, 'xxx')    app.MainLoop()
Mapping Modes

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.