How to create a basic drawing (Phoenix)

Keywords : Drawing, PaintDC, GCDC, Graphic context.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     def __init__(self, *args, **kwargs):
  13         wx.Panel.__init__(self, *args, **kwargs)
  14 
  15         self.Bind(wx.EVT_PAINT, self.OnPaint)
  16 
  17     #-----------------------------------------------------------------------
  18 
  19     def OnPaint(self, event):
  20         """
  21         ...
  22         """
  23 
  24         pdc = wx.PaintDC(self)
  25         gc = wx.GCDC(pdc)
  26         gc.Clear()
  27 
  28         #------------
  29         # Square.
  30         #------------
  31 
  32         gc.SetPen(wx.Pen("red", 2))
  33         gc.SetBrush(wx.Brush("yellow"))
  34 
  35         x = 100
  36         y = 30
  37         w = 50
  38         h = 50
  39 
  40         # pt, sz
  41         # or
  42         # rect
  43         # or
  44         # x, y, width, height
  45         gc.DrawRectangle(x , y, w, h)
  46         gc.DrawText("Square", int(x+w+10), int(y+h/2))
  47 
  48         #------------
  49         # Rectangle.
  50         #------------
  51 
  52         gc.SetPen(wx.Pen("black", 2))
  53         gc.SetBrush(wx.Brush((0, 255, 255, 255)))
  54 
  55         x = 100
  56         y = 130
  57         w = 100
  58         h = 50
  59 
  60         # pt, sz
  61         # or
  62         # rect
  63         # or
  64         # x, y, width, height
  65         gc.DrawRectangle(x , y, w, h)
  66         gc.DrawText("Rectangle", int(x+w+10), int(y+h/2))
  67 
  68         #------------
  69         # Rounded rectangle.
  70         #------------
  71 
  72         gc.SetPen(wx.Pen("black", 2))
  73         gc.SetBrush(wx.Brush((255, 0, 255, 128)))
  74 
  75         x = 100
  76         y = 230
  77         w = 100
  78         h = 50
  79         r = 8
  80 
  81         # pt, sz, radius
  82         # or
  83         # rect, radius
  84         # or
  85         # x, y, width, height, radius)
  86         gc.DrawRoundedRectangle(x, y, w, h, r)
  87         gc.DrawText("Rounded rectangle", int(x+w+10), int(y+h/2))
  88 
  89         #------------
  90         # Ellipse.
  91         #------------
  92 
  93         gc.SetPen(wx.Pen("gray", 2))
  94         gc.SetBrush(wx.Brush("green"))
  95 
  96         x = 100
  97         y = 330
  98         w = 100
  99         h = 50
 100 
 101         # pt, size
 102         # or
 103         # rect
 104         # or
 105         # x, y, width, height
 106         gc.DrawEllipse(x, y, w, h)
 107         gc.DrawText("Ellipse", int(x+w+10), int(y+h/2))
 108 
 109         #------------
 110         # Circle.
 111         #------------
 112 
 113         gc.SetPen(wx.Pen("red", 2))
 114         gc.SetBrush(wx.Brush("#ffc0cb"))
 115 
 116         x = 130
 117         y = 455
 118         r = 35
 119 
 120         # pt, radius
 121         # or
 122         # x, y, radius
 123         gc.DrawCircle(x, y, r)
 124         gc.DrawText("Circle", int(x+45), int(y))
 125 
 126         #------------
 127         # Line.
 128         #------------
 129 
 130         gc.SetPen(wx.Pen("purple", 4))
 131 
 132         x1 = 100
 133         y1 = 545
 134         x2 = 200
 135         y2 = 545
 136 
 137         # pt1, pt2
 138         # or
 139         # x1, y1, x2, y2
 140         gc.DrawLine(x1, y1, x2, y2)
 141         gc.DrawText("Line", int(x+w-10), int(y+80))
 142 
 143         #------------
 144         # Triangle.
 145         #------------
 146 
 147         gc.SetPen(wx.Pen("blue", 2))
 148         gc.SetBrush(wx.Brush((150, 150, 150, 128)))
 149 
 150         points = [(80, 80),
 151                   (10, 10),
 152                   (80, 10),
 153                   (80, 80)
 154                   ]
 155         x = 90
 156         y = 580
 157 
 158         # points, xoffset=0, yoffset=0, fill_style=ODDEVEN_RULE
 159         gc.DrawPolygon(points, x, y)
 160         gc.DrawText("Triangle", int(x+w-5), int(y+h/2))
 161 
 162 #---------------------------------------------------------------------------
 163 
 164 class MyFrame(wx.Frame):
 165     def __init__(self, *args, **kwargs):
 166         wx.Frame.__init__(self, *args, **kwargs)
 167 
 168         #------------
 169 
 170         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
 171 
 172         #------------
 173 
 174         self.Panel = MyPanel(self)
 175 
 176 #---------------------------------------------------------------------------
 177 
 178 
 179 class MyApp(wx.App):
 180     """
 181     ...
 182     """
 183     def OnInit(self):
 184 
 185         #------------
 186 
 187         frame = MyFrame(None, title="Sample_one", size=(380, 750))
 188         self.SetTopWindow(frame)
 189         frame.Show(True)
 190 
 191         return True
 192 
 193 #---------------------------------------------------------------------------
 194 
 195 def main():
 196     app = MyApp(False)
 197     app.MainLoop()
 198 
 199 #---------------------------------------------------------------------------
 200 
 201 if __name__ == "__main__" :
 202     main()


Sample two

img_sample_two.png

   1 # sample_two.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     def __init__(self, *args, **kwargs):
  13         wx.Panel.__init__(self, *args, **kwargs)
  14 
  15         self.Bind(wx.EVT_PAINT, self.OnPaint)
  16 
  17     #-----------------------------------------------------------------------
  18 
  19     def OnPaint(self, event):
  20         """
  21         ...
  22         """
  23 
  24         pdc = wx.PaintDC(self)
  25         gc = wx.GCDC(pdc)
  26         gc.Clear()
  27 
  28         #------------
  29         # Bitmap.
  30         #------------
  31 
  32         x = 100
  33         y = 30
  34 
  35         # bmp, pt, useMask=False
  36         # or
  37         # bitmap, x, y, useMask=False
  38         gc.DrawBitmap(wx.Bitmap("./Bitmaps/python.png"), x, y, True)
  39         gc.DrawText("Bitmap", int(x+150), int(y+20))
  40 
  41         #------------
  42         # Icon.
  43         #------------
  44 
  45         x = 100
  46         y = 130
  47 
  48         # icon, pt
  49         # or
  50         # icon, x, y
  51         gc.DrawIcon(wx.Icon("./icons/icon_wxWidgets.ico"), x, y)
  52         gc.DrawText("Icon", int(x+75), int(y+20))
  53 
  54         #------------
  55         # Arc.
  56         #------------
  57 
  58         gc.SetPen(wx.Pen("black", 2))
  59         gc.SetBrush(wx.Brush((204, 85, 248, 128)))
  60 
  61         x1 = 100
  62         y1 = 260
  63         x2 = 340
  64         y2 = 335
  65         x = 160
  66         y = 220
  67 
  68         # ptStart, ptEnd, centre
  69         # or
  70         # xStart, yStart, xEnd, yEnd, xc, yc
  71         gc.DrawArc(x1, y1, x2, y2, x, y)
  72         gc.DrawText("Arc", int(x+80), int(y+30))
  73 
  74         #------------
  75         # Polygon.
  76         #------------
  77 
  78         gc.SetPen(wx.Pen("blue", 2))
  79         gc.SetBrush(wx.Brush("#cacaca"))
  80 
  81         points = [(80, 140),
  82                   (130, 170),
  83                   (130, 140),
  84                   (170, 110)
  85                   ]
  86         x = 20
  87         y = 220
  88 
  89         # points, xoffset=0, yoffset=0, fill_style=ODDEVEN_RULE
  90         gc.DrawPolygon(points, x, y)
  91         gc.DrawText("Polygon", int(x+150), int(y+140))
  92 
  93         #------------
  94         # Spline.
  95         #------------
  96 
  97         gc.SetPen(wx.Pen("red", 2))
  98         gc.SetBrush(wx.Brush("pink"))
  99 
 100         points = [(100, 435),
 101                   (140, 435),
 102                   (145, 495),
 103                   (185, 495)
 104                   ]
 105 
 106         # x1, y1, x2, y2, x3, y3
 107         # or
 108         # points
 109         gc.DrawSpline(points)
 110         gc.DrawText("Spline", int(x+180), int(y+250))
 111 
 112         #------------
 113         # Point.
 114         #------------
 115 
 116         gc.SetPen(wx.Pen("purple", 80))
 117 
 118         x = 100
 119         y = 555
 120 
 121         # pt
 122         # or
 123         # x, y
 124         gc.DrawPoint (x, y)
 125         gc.DrawText("Point", int(x+20), int(y-10))
 126 
 127         #------------
 128         # Text.
 129         #------------
 130 
 131         x = 150
 132         y = 620
 133 
 134         gc.DrawText("Text", int(x+100), int(y))
 135 
 136         gc.SetTextForeground("red")
 137         font = wx.Font(36, wx.ROMAN, wx.ITALIC, wx.NORMAL)
 138         gc.SetFont(font)
 139 
 140         # text, pt
 141         # or
 142         # text, x, y
 143         gc.DrawText("Hello !", x-50, y-20)
 144 
 145 
 146 #---------------------------------------------------------------------------
 147 
 148 class MyFrame(wx.Frame):
 149     def __init__(self, *args, **kwargs):
 150         wx.Frame.__init__(self, *args, **kwargs)
 151 
 152         #------------
 153 
 154         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
 155 
 156         #------------
 157 
 158         self.Panel = MyPanel(self)
 159 
 160 #---------------------------------------------------------------------------
 161 
 162 
 163 class MyApp(wx.App):
 164     """
 165     ...
 166     """
 167     def OnInit(self):
 168 
 169         #------------
 170 
 171         frame = MyFrame(None, title="Sample_two", size=(380, 750))
 172         self.SetTopWindow(frame)
 173         frame.Show(True)
 174 
 175         return True
 176 
 177 #---------------------------------------------------------------------------
 178 
 179 def main():
 180     app = MyApp(False)
 181     app.MainLoop()
 182 
 183 #---------------------------------------------------------------------------
 184 
 185 if __name__ == "__main__" :
 186     main()


Sample three

img_sample_three.png

   1 # sample_three.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     def __init__(self, *args, **kwargs):
  13         wx.Panel.__init__(self, *args, **kwargs)
  14 
  15         self.Bind(wx.EVT_PAINT, self.OnPaint)
  16 
  17     #-----------------------------------------------------------------------
  18 
  19     def OnPaint(self, event):
  20         """
  21         ...
  22         """
  23 
  24         pdc = wx.PaintDC(self)
  25         gc = wx.GCDC(pdc)
  26         gc.Clear()
  27 
  28         #------------
  29         # Elliptic arc.
  30         #------------
  31 
  32         pen = wx.Pen("black", 2, wx.SOLID)
  33         pen.SetCap(wx.CAP_BUTT)
  34         gc.SetPen(pen)
  35         gc.SetBrush(wx.GREEN_BRUSH)
  36 
  37         x = 100
  38         y = 30
  39         w = 50
  40         h = 50
  41         r = 270
  42 
  43         # pt, sz, sa, ea
  44         # or
  45         # x, y , width, height, start, end
  46         gc.DrawEllipticArc(x, y, w, h, 0, r)
  47         gc.DrawText("Elliptic arc", int(x+w+10), int(y+h/2))
  48 
  49         #------------
  50         # Lines.
  51         #------------
  52 
  53         gc.SetPen(wx.Pen("purple", 4))
  54 
  55         points = [(20, 160),
  56                   (100, 160),
  57                   (20, 110),
  58                   (100, 110)
  59                   ]
  60         x = 90
  61         y = 20
  62 
  63         # points, xoffset=0, yoffset=0
  64         gc.DrawLines(points, x, y)
  65         gc.DrawText("Lines", int(x+120), int(y+125))
  66 
  67         #------------
  68         # Gradient fill linear.
  69         #------------
  70 
  71         x = 100
  72         y = 240
  73         w = 100
  74         h = 100
  75 
  76         gc.GradientFillLinear((x, y, w, h),
  77                               "#ff6b1a",
  78                               "#000000",
  79                               wx.NORTH)
  80         # rect, initialColour, destColour, nDirection=RIGHT)
  81         gc.DrawText("Gradient fill linear", int(x+115), int(y+40))
  82 
  83         #------------
  84         # Gradient fill concentric.
  85         #------------
  86 
  87         x = 100
  88         y = 380
  89         w = 100
  90         h = 100
  91 
  92 
  93         # Note : currently this function is very slow,
  94         # don’t use it for real-time drawing.
  95 
  96         # rect, initialColour, destColour
  97         # or
  98         # rect, initialColour, destColour
  99         gc.GradientFillConcentric((x, y, w, h),
 100                                   "#61c3ff",
 101                                   "#000000",
 102                                   (50, 50))
 103         # rect, initialColour, destColour, circleCenter)
 104         gc.DrawText("Gradient fill concentric", int(x+115), int(y+40))
 105 
 106 #---------------------------------------------------------------------------
 107 
 108 class MyFrame(wx.Frame):
 109     def __init__(self, *args, **kwargs):
 110         wx.Frame.__init__(self, *args, **kwargs)
 111 
 112         #------------
 113 
 114         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
 115 
 116         #------------
 117 
 118         self.Panel = MyPanel(self)
 119 
 120 #---------------------------------------------------------------------------
 121 
 122 
 123 class MyApp(wx.App):
 124     """
 125     ...
 126     """
 127     def OnInit(self):
 128 
 129         #------------
 130 
 131         frame = MyFrame(None, title="Sample_three", size=(380, 580))
 132         self.SetTopWindow(frame)
 133         frame.Show(True)
 134 
 135         return True
 136 
 137 #---------------------------------------------------------------------------
 138 
 139 def main():
 140     app = MyApp(False)
 141     app.MainLoop()
 142 
 143 #---------------------------------------------------------------------------
 144 
 145 if __name__ == "__main__" :
 146     main()


Sample four

img_sample_four.png

   1 # sample_four.py
   2 
   3 import wx
   4 
   5 # class MyPanel
   6 # class MyFrame
   7 # class MyApp
   8 
   9 #---------------------------------------------------------------------------
  10 
  11 class MyPanel(wx.Panel):
  12     def __init__(self, *args, **kw):
  13         wx.Panel.__init__(self, *args, **kw)
  14 
  15         self.Bind(wx.EVT_PAINT, self.OnPaint)
  16 
  17     #-----------------------------------------------------------------------
  18 
  19     def OnPaint(self, event):
  20         """
  21         ...
  22         """
  23 
  24         gc = wx.GraphicsContext.Create(wx.PaintDC(self))
  25 
  26         pen = wx.Pen("navy", 2)
  27         gc.SetPen(pen)
  28         brush = wx.Brush((255,32,32,128))
  29         gc.SetBrush(brush)
  30         gc.PushState()
  31 
  32         path = gc.CreatePath()
  33         path.MoveToPoint(50, 50)
  34         path.AddLineToPoint(25,25)
  35         path.AddLineToPoint(50,25)
  36         path.AddLineToPoint(50,50)
  37         path.CloseSubpath()
  38 
  39         gc.DrawPath(path)
  40 
  41         gc.Scale(2,2)
  42         gc.Translate(10,5)
  43         gc.DrawPath(path)
  44 
  45         gc.Translate(50,0)
  46         gc.FillPath(path)
  47         gc.Translate(0,5)
  48         gc.StrokePath(path)
  49         gc.Translate(0,5)
  50 
  51         brush = wx.Brush((32,32,255,128))
  52         gc.SetBrush(brush)
  53 
  54         gc.FillPath(path)
  55         gc.Translate(50,0)
  56         gc.DrawPath(path)
  57 
  58         gc.PopState()
  59 
  60         points = [ (5.2, 5.9),
  61                    (50, 50),
  62                    (35, 50),
  63                    (25,40),
  64                    wx.Point2D(20.5,50.9),
  65                    wx.Point2D(5,25),
  66                    (5,6)
  67                    ]
  68 
  69         gc.Translate(40, 150)   #
  70         gc.DrawLines(points)
  71         gc.Translate(75, 0)
  72         gc.StrokeLines(points)
  73 
  74         begin = [ (0,0),
  75                   (0,10),
  76                   (0,20),
  77                   (0,30),
  78                   (0,40),
  79                   (0,50),
  80                   ]
  81 
  82         end   = [ (50,0),
  83                   (50,10),
  84                   (50,20),
  85                   (50,30),
  86                   (50,40),
  87                   (50,50),
  88                   ]
  89 
  90         # In a floating point coordinate system the center of the
  91         # pixel is actually at x+0.5, y+0.5, so with anti-aliasing
  92         # turned on we'll get a crisper line by positioning our line
  93         # segments at the 0.5 offset.  For this test we'll just let
  94         # the GC do the translation for us.
  95         gc.Translate(0.5, 0.5)
  96 
  97         pen = wx.Pen("purple", 1)
  98         gc.SetPen(pen)
  99 
 100         gc.Translate(75, 0)
 101         gc.StrokeLineSegments(begin, end)
 102 
 103         gc.Translate(75, 0)
 104         gc.Scale(2,2)
 105         gc.StrokeLineSegments(begin, end)
 106         gc.DrawLines(points)
 107 
 108         del path
 109 
 110 #---------------------------------------------------------------------------
 111 
 112 class MyFrame(wx.Frame):
 113     def __init__(self, *args, **kwargs):
 114         wx.Frame.__init__(self, *args, **kwargs)
 115 
 116         #------------
 117 
 118         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
 119 
 120         #------------
 121 
 122         self.Panel = MyPanel(self)
 123 
 124 #---------------------------------------------------------------------------
 125 
 126 
 127 class MyApp(wx.App):
 128     """
 129     ...
 130     """
 131     def OnInit(self):
 132 
 133         #------------
 134 
 135         frame = MyFrame(None, title="Sample_four", size=(420, 320))
 136         self.SetTopWindow(frame)
 137         frame.Show(True)
 138 
 139         return True
 140 
 141 #---------------------------------------------------------------------------
 142 
 143 def main():
 144     app = MyApp(False)
 145     app.MainLoop()
 146 
 147 #---------------------------------------------------------------------------
 148 
 149 if __name__ == "__main__" :
 150     main()


Sample five

img_sample_five.png

   1 # sample_five.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program performs set operations on regions.
   8 
   9 Author      : Jan Bodnar
  10 Website     : zetcode.com
  11 link        : http://www.zetcode.com/wxpython/gdi/
  12 Last edited : May 2018
  13 
  14 --------
  15 
  16 Region operations :
  17 
  18 Regions can be combined to create more complex shapes.
  19 We can use four set operations : union, intersect, substract, xor.
  20 
  21 The following example shows all four operations in action.
  22 
  23 --------
  24 
  25 In the example, we present six region set operations.
  26 
  27 region1 = wx.Region(100, 20, 50, 50)
  28 region2 = wx.Region(110, 40, 50, 50)
  29 region1.Intersect(region2)
  30 
  31 """
  32 
  33 import wx
  34 
  35 # class MyFrame
  36 # class MyApp
  37 
  38 #---------------------------------------------------------------------------
  39 
  40 class MyFrame(wx.Frame):
  41     def __init__(self, *args, **kw):
  42         super(MyFrame, self).__init__(*args, **kw)
  43 
  44         #------------
  45 
  46         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  47         self.SetSize((300, 250))
  48         self.SetMinSize((300, 250))
  49         self.SetBackgroundColour("yellow")
  50 
  51         #------------
  52 
  53         # Simplified init method.
  54         self.InitUI()
  55 
  56     #-----------------------------------------------------------------------
  57 
  58     def InitUI(self):
  59         """
  60         ...
  61         """
  62 
  63         self.Bind(wx.EVT_PAINT, self.OnPaint)
  64 
  65         self.SetTitle("Sample_five (regions)")
  66         self.Centre()
  67 
  68 
  69     def OnPaint(self, event):
  70         """
  71         ...
  72         """
  73 
  74         dc = wx.PaintDC(self)
  75         dc.SetPen(wx.Pen('#000000'))
  76 
  77         dc.DrawRectangle(20, 20, 50, 50)
  78         dc.DrawRectangle(30, 40, 50, 50)
  79 
  80         dc.SetBrush(wx.Brush('#ffffff'))
  81         dc.DrawRectangle(100, 20, 50, 50)
  82         dc.DrawRectangle(110, 40, 50, 50)
  83 
  84         region1 = wx.Region(100, 20, 50, 50)
  85         region2 = wx.Region(110, 40, 50, 50)
  86         region1.Intersect(region2)
  87 
  88         rect = region1.GetBox()
  89         dc.SetDeviceClippingRegion(region1)
  90         dc.SetBrush(wx.Brush('#ff0000'))
  91         dc.DrawRectangle(rect)
  92         dc.DestroyClippingRegion()
  93 
  94         dc.SetBrush(wx.Brush('#ffffff'))
  95         dc.DrawRectangle(180, 20, 50, 50)
  96         dc.DrawRectangle(190, 40, 50, 50)
  97 
  98         region1 = wx.Region(180, 20, 50, 50)
  99         region2 = wx.Region(190, 40, 50, 50)
 100         region1.Union(region2)
 101         dc.SetDeviceClippingRegion(region1)
 102 
 103         rect = region1.GetBox()
 104         dc.SetBrush(wx.Brush('#fa8e00'))
 105         dc.DrawRectangle(rect)
 106         dc.DestroyClippingRegion()
 107 
 108         dc.SetBrush(wx.Brush('#ffffff'))
 109         dc.DrawRectangle(20, 120, 50, 50)
 110         dc.DrawRectangle(30, 140, 50, 50)
 111         region1 = wx.Region(20, 120, 50, 50)
 112         region2 = wx.Region(30, 140, 50, 50)
 113         region1.Xor(region2)
 114 
 115         rect = region1.GetBox()
 116         dc.SetDeviceClippingRegion(region1)
 117         dc.SetBrush(wx.Brush('#619e1b'))
 118         dc.DrawRectangle(rect)
 119         dc.DestroyClippingRegion()
 120 
 121         dc.SetBrush(wx.Brush('#ffffff'))
 122         dc.DrawRectangle(100, 120, 50, 50)
 123         dc.DrawRectangle(110, 140, 50, 50)
 124         region1 = wx.Region(100, 120, 50, 50)
 125         region2 = wx.Region(110, 140, 50, 50)
 126         region1.Subtract(region2)
 127 
 128         rect = region1.GetBox()
 129         dc.SetDeviceClippingRegion(region1)
 130         dc.SetBrush(wx.Brush('#715b33'))
 131         dc.DrawRectangle(rect)
 132         dc.DestroyClippingRegion()
 133 
 134         dc.SetBrush(wx.Brush('#ffffff'))
 135         dc.DrawRectangle(180, 120, 50, 50)
 136         dc.DrawRectangle(190, 140, 50, 50)
 137         region1 = wx.Region(180, 120, 50, 50)
 138         region2 = wx.Region(190, 140, 50, 50)
 139         region2.Subtract(region1)
 140 
 141         rect = region2.GetBox()
 142         dc.SetDeviceClippingRegion(region2)
 143         dc.SetBrush(wx.Brush('#0d0060'))
 144         dc.DrawRectangle(rect)
 145         dc.DestroyClippingRegion()
 146 
 147 #---------------------------------------------------------------------------
 148 
 149 class MyApp(wx.App):
 150     def OnInit(self):
 151 
 152         #------------
 153 
 154         frame = MyFrame(None)
 155         self.SetTopWindow(frame)
 156         frame.Show(True)
 157 
 158         return True
 159 
 160 #---------------------------------------------------------------------------
 161 
 162 def main():
 163     app = MyApp(False)
 164     app.MainLoop()
 165 
 166 #---------------------------------------------------------------------------
 167 
 168 if __name__ == "__main__" :
 169     main()


Sample six

img_sample_six.png

   1 # sample_six.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program draws uses different
   8 joins and caps in drawing.
   9 
  10 Author : Jan Bodnar
  11 Website : zetcode.com
  12 Last edited : May 2018
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class Example
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class Example(wx.Frame):
  25     def __init__(self, *args, **kw):
  26         super(Example, self).__init__(*args, **kw)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         """
  34         ...
  35         """
  36 
  37         self.Bind(wx.EVT_PAINT, self.OnPaint)
  38 
  39         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  40         self.SetTitle("Sample_six (joins and caps)")
  41         self.SetSize((350, 310))
  42         self.SetMinSize((350, 310))
  43         self.SetBackgroundColour("#8de578")
  44         self.Centre()
  45 
  46 
  47     def OnPaint(self, event):
  48         """
  49         ...
  50         """
  51 
  52         dc = wx.PaintDC(self)
  53         dc = wx.GCDC(dc)
  54 
  55         pen = wx.Pen('#4c4c4c', 10, wx.SOLID)
  56 
  57         pen.SetJoin(wx.JOIN_MITER)
  58         dc.SetPen(pen)
  59         dc.DrawRectangle(15, 15, 80, 50)
  60 
  61         pen.SetJoin(wx.JOIN_BEVEL)
  62         dc.SetPen(pen)
  63         dc.DrawRectangle(125, 15, 80, 50)
  64 
  65         pen.SetJoin(wx.JOIN_ROUND)
  66         dc.SetPen(pen)
  67         dc.DrawRectangle(235, 15, 80, 50)
  68 
  69         pen.SetCap(wx.CAP_BUTT)
  70         dc.SetPen(pen)
  71         dc.DrawLine(30, 150,  150, 150)
  72 
  73         pen.SetCap(wx.CAP_PROJECTING)
  74         dc.SetPen(pen)
  75         dc.DrawLine(30, 190,  150, 190)
  76 
  77         pen.SetCap(wx.CAP_ROUND)
  78         dc.SetPen(pen)
  79         dc.DrawLine(30, 230,  150, 230)
  80 
  81         pen2 = wx.Pen('gray', 1, wx.SOLID)
  82         dc.SetPen(pen2)
  83         dc.DrawLine(30, 130, 30, 250)
  84         dc.DrawLine(150, 130, 150, 250)
  85         dc.DrawLine(155, 130, 155, 250)
  86 
  87 #---------------------------------------------------------------------------
  88 
  89 def main():
  90     app = wx.App()
  91     ex = Example(None)
  92     ex.Show()
  93     app.MainLoop()
  94 
  95 #---------------------------------------------------------------------------
  96 
  97 if __name__ == '__main__':
  98     main()


Sample seven

img_sample_seven.png

   1 # sample_seven.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program draws six rectangles
   8 with different pens.
   9 
  10 Author : Jan Bodnar
  11 Website : zetcode.com
  12 Last edited : May 2018
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class Example
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class Example(wx.Frame):
  25     def __init__(self, *args, **kw):
  26         super(Example, self).__init__(*args, **kw)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         """
  34         ...
  35         """
  36 
  37         self.Bind(wx.EVT_PAINT, self.OnPaint)
  38 
  39         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  40         self.SetTitle("Sample_seven (pens)")
  41         self.SetSize((370, 310))
  42         self.SetMinSize((370, 310))
  43         self.SetBackgroundColour("#e1e0d9")
  44         self.Centre()
  45 
  46 
  47     def OnPaint(self, event):
  48         """
  49         ...
  50         """
  51 
  52         dc = wx.PaintDC(self)
  53         dc = wx.GCDC(dc)
  54 
  55         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.SOLID))
  56         dc.DrawRectangle(10, 15, 90, 60)
  57 
  58         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.DOT))
  59         dc.DrawRectangle(130, 15, 90, 60)
  60 
  61         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.LONG_DASH))
  62         dc.DrawRectangle(250, 15, 90, 60)
  63 
  64         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.SHORT_DASH))
  65         dc.DrawRectangle(10, 105, 90, 60)
  66 
  67         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.DOT_DASH))
  68         dc.DrawRectangle(130, 105, 90, 60)
  69 
  70         dc.SetPen(wx.Pen('#4c4c4c', 2, wx.TRANSPARENT))
  71         dc.DrawRectangle(250, 105, 90, 60)
  72 
  73         pen = wx.Pen('#4c4c4c', 2, wx.PENSTYLE_USER_DASH)
  74         pen.SetDashes([2, 5, 2, 2])
  75         dc.SetPen(pen)
  76         dc.DrawRectangle(10, 195, 90, 60)
  77 
  78 #---------------------------------------------------------------------------
  79 
  80 def main():
  81     app = wx.App()
  82     ex = Example(None)
  83     ex.Show()
  84     app.MainLoop()
  85 
  86 #---------------------------------------------------------------------------
  87 
  88 if __name__ == '__main__':
  89     main()


Sample eight

img_sample_eight.png

   1 # sample_eight.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program draws nine coloured
   8 rectangles on the window.
   9 
  10 Author: Jan Bodnar
  11 Website: zetcode.com
  12 Last edited: May 2018
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class Example
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class Example(wx.Frame):
  25     def __init__(self, *args, **kw):
  26         super(Example, self).__init__(*args, **kw)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         """
  34         ...
  35         """
  36 
  37         self.Bind(wx.EVT_PAINT, self.OnPaint)
  38 
  39         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  40         self.SetTitle("Sample_eight (colours)")
  41         self.SetSize((400, 310))
  42         self.SetMinSize((400, 310))
  43         self.SetBackgroundColour("lightgray")
  44         self.Centre()
  45 
  46 
  47     def OnPaint(self, event):
  48         """
  49         ...
  50         """
  51 
  52         dc = wx.PaintDC(self)
  53         dc.SetPen(wx.Pen('#d4d4d4'))
  54 
  55         dc.SetBrush(wx.Brush('#c56c00'))
  56         dc.DrawRectangle(35, 15, 90, 60)
  57 
  58         dc.SetBrush(wx.Brush('#1ac500'))
  59         dc.DrawRectangle(147, 15, 90, 60)
  60 
  61         dc.SetBrush(wx.Brush('#539e47'))
  62         dc.DrawRectangle(259, 15, 90, 60)
  63 
  64         dc.SetBrush(wx.Brush('#004fc5'))
  65         dc.DrawRectangle(35, 105, 90, 60)
  66 
  67         dc.SetBrush(wx.Brush('#c50024'))
  68         dc.DrawRectangle(147, 105, 90, 60)
  69 
  70         dc.SetBrush(wx.Brush('#9e4757'))
  71         dc.DrawRectangle(259, 105, 90, 60)
  72 
  73         dc.SetBrush(wx.Brush('#5f3b00'))
  74         dc.DrawRectangle(35, 195, 90, 60)
  75 
  76         dc.SetBrush(wx.Brush('#4c4c4c'))
  77         dc.DrawRectangle(147, 195, 90, 60)
  78 
  79         dc.SetBrush(wx.Brush('#785f36'))
  80         dc.DrawRectangle(259, 195, 90, 60)
  81 
  82 #---------------------------------------------------------------------------
  83 
  84 def main():
  85     app = wx.App()
  86     ex = Example(None)
  87     ex.Show()
  88     app.MainLoop()
  89 
  90 #---------------------------------------------------------------------------
  91 
  92 if __name__ == '__main__':
  93     main()


Sample nine

img_sample_nine.png

   1 # sample_nine.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program draws eight rectangles
   8 filled with different brushes.
   9 
  10 Author: Jan Bodnar
  11 Website: zetcode.com
  12 Last edited: May 2018
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class Example
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class Example(wx.Frame):
  25     def __init__(self, *args, **kw):
  26         super(Example, self).__init__(*args, **kw)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         """
  34         ...
  35         """
  36 
  37         self.Bind(wx.EVT_PAINT, self.OnPaint)
  38 
  39         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  40         self.SetTitle("Sample_nine (brushes)")
  41         self.SetSize((400, 310))
  42         self.SetMinSize((400, 310))
  43         self.SetBackgroundColour("#78c2e5")
  44         self.Centre()
  45 
  46 
  47     def OnPaint(self, event):
  48         """
  49         ...
  50         """
  51 
  52         dc = wx.PaintDC(self)
  53 
  54         dc.SetBrush(wx.Brush('#4c4c4c', wx.CROSS_HATCH))
  55         dc.DrawRectangle(35, 15, 90, 60)
  56 
  57         dc.SetBrush(wx.Brush('#4c4c4c', wx.SOLID))
  58         dc.DrawRectangle(147, 15, 90, 60)
  59 
  60         dc.SetBrush(wx.Brush('#4c4c4c', wx.BDIAGONAL_HATCH))
  61         dc.DrawRectangle(259, 15, 90, 60)
  62 
  63         dc.SetBrush(wx.Brush('#4c4c4c', wx.CROSSDIAG_HATCH))
  64         dc.DrawRectangle(35, 105, 90, 60)
  65 
  66         dc.SetBrush(wx.Brush('#4c4c4c', wx.FDIAGONAL_HATCH))
  67         dc.DrawRectangle(147, 105, 90, 60)
  68 
  69         dc.SetBrush(wx.Brush('#4c4c4c', wx.HORIZONTAL_HATCH))
  70         dc.DrawRectangle(259, 105, 90, 60)
  71 
  72         dc.SetBrush(wx.Brush('#4c4c4c', wx.VERTICAL_HATCH))
  73         dc.DrawRectangle(35, 195, 90, 60)
  74 
  75         dc.SetBrush(wx.Brush('#4c4c4c', wx.TRANSPARENT))
  76         dc.DrawRectangle(147, 195, 90, 60)
  77 
  78 #---------------------------------------------------------------------------
  79 
  80 def main():
  81     app = wx.App()
  82     ex = Example(None)
  83     ex.Show()
  84     app.MainLoop()
  85 
  86 #---------------------------------------------------------------------------
  87 
  88 if __name__ == '__main__':
  89     main()


Sample ten

img_sample_ten.png

   1 # sample_ten.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 This program draws three rectangles
   8 with custom brush patterns.
   9 
  10 Author: Jan Bodnar
  11 Website: zetcode.com
  12 Last edited: May 2018
  13 Link : http://zetcode.com/wxpython/gdi/
  14 
  15 """
  16 
  17 import wx
  18 
  19 # class Example
  20 # def main
  21 
  22 #---------------------------------------------------------------------------
  23 
  24 class Example(wx.Frame):
  25     def __init__(self, *args, **kw):
  26         super(Example, self).__init__(*args, **kw)
  27 
  28         self.InitUI()
  29 
  30     #-----------------------------------------------------------------------
  31 
  32     def InitUI(self):
  33         """
  34         ...
  35         """
  36 
  37         self.Bind(wx.EVT_PAINT, self.OnPaint)
  38 
  39         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico'))
  40         self.SetTitle("Sample_ten (custom patterns)")
  41         self.SetSize((400, 140))
  42         self.SetMinSize((400, 140))
  43         self.SetBackgroundColour("white")
  44         self.Centre()
  45 
  46 
  47     def OnPaint(self, event):
  48         """
  49         ...
  50         """
  51 
  52         dc = wx.PaintDC(self)
  53 
  54         dc.SetPen(wx.Pen('#C7C3C3'))
  55 
  56         brush1 = wx.Brush(wx.Bitmap('./Bitmaps/pattern1.png'))
  57         dc.SetBrush(brush1)
  58         dc.DrawRectangle(35, 15, 90, 60)
  59 
  60         brush2 = wx.Brush(wx.Bitmap('./Bitmaps/pattern2.png'))
  61         dc.SetBrush(brush2)
  62         dc.DrawRectangle(147, 15, 90, 60)
  63 
  64         brush3 = wx.Brush(wx.Bitmap('./Bitmaps/pattern3.png'))
  65         dc.SetBrush(brush3)
  66         dc.DrawRectangle(259, 15, 90, 60)
  67 
  68 #---------------------------------------------------------------------------
  69 
  70 def main():
  71     app = wx.App()
  72     ex = Example(None)
  73     ex.Show()
  74     app.MainLoop()
  75 
  76 #---------------------------------------------------------------------------
  77 
  78 if __name__ == '__main__':
  79     main()


Download source

source.zip


Additional Information

Link :

http://zetcode.com/wxpython/gdi/

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Cody Precord (sample_four.py coding), Jan Bodnar (sample_five / six / seven / eight / nine / ten.py coding), Chris Barker, the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

11/01/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah...

How to create a basic drawing (Phoenix) (last edited 2020-12-14 14:16:47 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.