Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing to Panel inside of Frame in wxPython

Below, I have a panel inside of a frame. Why am I not able to draw to the panel? I just get a plain white screen. If I get rid of the panel and draw directly to the frame...it works. any help would be appreciated.

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'window',(200,200),(600,600))
        self.Center()
        self.panel=wx.Panel(self)
        self.panel.SetBackgroundColour('white')
        self.firstpoint=wx.Point(300,300)
        self.secondpoint=wx.Point(400,400)
        self.Bind(wx.EVT_PAINT,self.onPaint)


    def onPaint(self,event):
        dc=wx.PaintDC(self.panel)
        dc.DrawLine(self.firstpoint.x,self.firstpoint.y,
                    self.secondpoint.x,self.secondpoint.y)
like image 552
BPoy Avatar asked Sep 10 '14 03:09

BPoy


1 Answers

Try binding the event to the panel, not to the whole frame:

self.panel.Bind(wx.EVT_PAINT, self.onPaint)

Your version kind-of work for me (windows), but it keeps redrawing the panel so it eats up the whole processor.

From documentation: Note that In a paint event handler, the application must always create a wxPaintDC object, even if you do not use it. Otherwise, under MS Windows, refreshing for this and other windows will go wrong.

Here, you received the paint event for the whole frame but used the dc for the panel.

EDIT: This http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind explains quite nicely why this won't work:

self.Bind(wx.EVT_PAINT, self.onPaint, self.panel)

In this case the onPaint handler is never called.

like image 87
Petr Blahos Avatar answered Sep 28 '22 18:09

Petr Blahos