wxPython - Drawing an unfilled rectangle with the DC

11,665

Solution 1

This makes a grey rectangle:

import wx

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel to draw on, inherits wx.Panel """
    def __init__(self, parent, id):
        # create a panel
        wx.Panel.__init__(self, parent, id)
        self.SetBackgroundColour("white")
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, evt):
        """set up the device context (DC) for painting"""
        self.dc = wx.PaintDC(self)
        self.dc.BeginDrawing()
        self.dc.SetPen(wx.Pen("grey",style=wx.TRANSPARENT))
        self.dc.SetBrush(wx.Brush("grey", wx.SOLID))
        # set x, y, w, h for rectangle
        self.dc.DrawRectangle(250,250,50, 50)
        self.dc.EndDrawing()
        del self.dc

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
frame = wx.Frame(None, -1, "Drawing A Rectangle...", size = (500, 500))
# call the derived class, -1 is default ID
MyPanel(frame,-1)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()

Solution 2

In order to draw a non-filled rectangle you need to set brush transparent as the brush does the filling and the pen draws the outline. The example below draws a blue non-filled rectangle alongside a red filled one.

import wx

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel to draw on, inherits wx.Panel """

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        self.SetBackgroundColour("white")
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        """set up the device context (DC) for painting"""
        dc = wx.PaintDC(self)

        #blue non-filled rectangle
        dc.SetPen(wx.Pen("blue"))
        dc.SetBrush(wx.Brush("blue", wx.TRANSPARENT)) #set brush transparent for non-filled rectangle
        dc.DrawRectangle(10,10,200,200)

        #red filled rectangle
        dc.SetPen(wx.Pen("red"))
        dc.SetBrush(wx.Brush("red"))
        dc.DrawRectangle(220,10,200,200)

app = wx.App()
frame = wx.Frame(None, -1, "Drawing A Rectangle...", size=(460, 300))
MyPanel(frame,-1)
frame.Show()        
frame.Centre()
app.MainLoop()
Share:
11,665
Bibendum
Author by

Bibendum

Updated on June 04, 2022

Comments

  • Bibendum
    Bibendum almost 2 years
    dc.SetPen(wx.Pen(wx.BLACK, 0))
    dc.SetBrush(wx.Brush("C0C0C0"))
    dc.DrawRectangle(50,50,50,50)
    

    This is my best attempt at drawing a 50x50, gray box with no border. However, setting the pen width to 0 doesn't seem to accomplish anything, and setting the brush only changes the fill from pure white to pure black.

    Here's it in the context of a panel, in case it's part of the problem:

    class DrawRect(wx.Panel):
         def __init__(self,parent=None,id=-1,pos=(-1,-1),size=(-1,-1),style=0):
             wx.Panel.__init__(self,parent,id,size,pos,style)
             self.SetBackgroundColour("#D18B47")
             self.Bind(wx.EVT_PAINT,self.onPaint)
    
         def onPaint(self, event):
             event.Skip()
             dc = wx.PaintDC(event.GetEventObject())
             self.drawRect(dc)
    
         def drawRect(self,dc):
             dc.SetPen(wx.Pen("FFCE8A", 0))
             dc.SetBrush(wx.Brush("C0C0C0"))
             dc.DrawRectangle(50,50,50,50)
    
  • Steven Sproat
    Steven Sproat about 14 years
    small note: you could do dc.SetPen(wx.TRANSPARENT_PEN) for smaller code, and there's no need to delete the DC. In fact, it's probably better not to store it as a class attribute.
  • Barton
    Barton over 10 years
    "wx.DC.BeginDrawing and EndDrawing have been deprecated..." since 18-Oct-2006 <see="wxpython.org/recentchanges.php">