Note that there are some explanatory texts on larger screens.

plurals
  1. POPanel doesn't fully refresh when using a custom background in wxPython
    text
    copied!<p>Here's some sample code:</p> <pre><code>import wx class MainPanel(wx.Panel): p1 = None p2 = None def __init__(self, parent): wx.Panel.__init__(self, parent=parent) self.frame = parent #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) #self.bg = wx.Bitmap("2.jpg") self.Bind(wx.EVT_MOTION, self.OnMouseMove) self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown) self.Bind(wx.EVT_PAINT, self.OnPaint) self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) def OnMouseMove(self, event): if event.Dragging() and event.LeftIsDown(): self.p2 = event.GetPosition() self.Refresh() def OnMouseDown(self, event): self.p1 = event.GetPosition() def OnPaint(self, event): if self.p1 is None or self.p2 is None: return dc = wx.PaintDC(self) dc.SetPen(wx.Pen('red', 3, wx.LONG_DASH)) dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.SOLID)) dc.DrawRectangle(self.p1.x, self.p1.y, self.p2.x - self.p1.x, self.p2.y - self.p1.y) class MainFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, size=(600,450)) panel = MainPanel(self) self.Center() class Main(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self, redirect, filename) dlg = MainFrame() dlg.Show() if __name__ == "__main__": app = Main() app.MainLoop() </code></pre> <p>The two offending lines are:</p> <pre><code> #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) #self.bg = wx.Bitmap("2.jpg") </code></pre> <p>If you run the code like that, you're able to draw a rectangle on the panel. If you start a new rectangle, the old one disappears, because the panel is refreshing. </p> <p>However, if you uncomment those two lines, when you draw a rectangle, it stays there. You can have infinite number of rectangles. If I use dc.Clear(), the background disappears and reloading the background makes the application slow and it flickers.</p> <p>How do I make the panel fully refresh while using a custom background?</p> <p>By the way, the background image doesn't load with this code, but the behavior is the same.</p> <p><strong>EDIT:</strong> I found a workaround to the flickering caused by using dc.Clear() and reloading the background. Setting double buffering on the panel to true solves the flickering:</p> <pre><code>self.SetDoubleBuffered(True) </code></pre> <p>I will use that, but I'll leave the question open in case someone knows the answer to the initial problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload