Search code examples
pythonwxpython

How to make a canvas (rectangle) in wxpython?


I am trying to make a windows canvas type rectangle, here is an image if you are having issues understanding what i mean,

a busy cat

Is there any way todo this in wxpython? ( also, is there a way to set it to automatically adjust to the window width -20px?, so a radius around the window, and will adjust to the users window size.


Solution

  • EDIT: I asked on the wxPython IRC channel and a fellow named "r4z" came up with the following edit to my code which worked for me on Windows 7.

    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
            self.Bind(wx.EVT_PAINT, self.OnPaint)
    
        #----------------------------------------------------------------------
        def OnPaint(self, event):
            """"""
            pdc = wx.PaintDC(self)
            try:
                dc = wx.GCDC(pdc)
            except:
                dc = pdc
    
            w, h = self.GetSizeTuple()
            w = w - 10
            h = h - 10
    
            dc.Clear()
            dc.DrawRectangle(x=5, y=5, width=w, height=h)
    
    
    #----------------------------------------------------------------------
    def OnSize(event):
        event.EventObject.Refresh()
        event.Skip()
    
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = wx.Frame(None, title="Test")
        panel = MyPanel(frame)
        frame.Bind(wx.EVT_SIZE, OnSize)
        frame.Show()
        app.MainLoop()
    

    Alternately, you might look at the wx.StaticBox widget.

    EDIT #2: You could also just set the frame's style like this and skip the whole OnSize business:

    frame = wx.Frame(None, title="Test", style=wx.DEFAULT_FRAME_STYLE|wx.FULL_REPAINT_ON_RESIZE)