Search code examples
pythonwxpythonlinedraw

Why will nothing be drawn on my client area of the window - wxpython


I am trying to draw a simple line on the client area of a frame. I can't figure out why nothing will draw. I usually use Pygame for this stuff.

Here is my code:

import wx
sigwin = wx.Frame(None,title = "Name of Window")
sigwin.SetSize(-1,-1,600,200))
sigwin.SetBackgroundColour((255,255,255))
txt = wx.StaticText(sigwin, id=-1, label='Please sign your name in the box below:',pos=(20,40),style=wx.ALIGN_CENTRE_HORIZONTAL)
font3 = wx.Font(15, wx.DECORATIVE, wx.NORMAL, wx.NORMAL,0,'Comic Sans MS')
txt.SetFont(font3)
sigwin.Centre()

def on_paint(event):
    dc = wx.ClientDC(event.GetEventObject())
    dc.Clear()
    dc.SetPen(wx.Pen("BLACK", 0))
    dc.DrawLine(0, 0, 500, 500)
    
sigwin.Bind(wx.EVT_PAINT, on_paint)
sigwin.Show()

Solution

  • Since you're using a GUI library, it's processing the paint event after you do. In this case, you're drawing a line then wx is drawing the text box on top.

    This code removes the text box and shows your line:

    import wx
    app = wx.App(False)
    
    sigwin = wx.Frame(None,title = "Name of Window")
    sigwin.SetSize(-1,-1,600,200)
    sigwin.SetBackgroundColour((255,255,255))
    #txt = wx.StaticText(sigwin, id=-1, label='Please sign your name in the box below:',pos=(20,40),style=wx.ALIGN_CENTRE_HORIZONTAL)
    #font3 = wx.Font(15, wx.DECORATIVE, wx.NORMAL, wx.NORMAL,0,'Comic Sans MS')
    #txt.SetFont(font3)
    sigwin.Centre()
    
    def on_paint(event):
        dc = wx.ClientDC(event.GetEventObject())
        dc.Clear()
        dc.SetPen(wx.Pen("BLACK", 0))
        dc.DrawLine(0, 0, 500, 500)
        print('paint')
        
    sigwin.Bind(wx.EVT_PAINT, on_paint)
    sigwin.Show()
    
    #frame = sigwin(None, "Sample editor")
    app.MainLoop()