How to avoid flickering when drawing rubberband?


I have Windows 10, Python 3.9.0 64-bit and wxpython 4.1.1.

In wxpython demo, there is “overlay” demo, in which user draws a rubberband. But while the rubberband is being drawn, the rubberband flickers. Does anyone know how to avoid the flickering?


Hi, does anybody have any ideas how to fix flickering mentioned above?


I don’t know if it’s the same on Windows 10, but on Linux if I comment out lines 112 & 113 from the demo, the flickering disappears:

            # if 'wxMac' not in wx.PlatformInfo:
            #     dc = wx.GCDC(dc)

However, the comment before these lines says “If you do not need to use a semi-transparent background then you can leave this out”, so presumably there could be a problem if you do need to use a semi-transparent background?


I found the solution while tweaking the demo code and posted here. I don’t understand why dc = wx.GCDC(dc) is required for transparency but it works on Windows. I hope someone explains.


Your example uses:

dc = wx.BufferedDC(dc, self.bitmap)

I don’t want to draw on a bitmap. So, when I comment out the line above, the flickering comes back. How to solve this?

Yes, the rubberband must be transparent.

On linux, the appearance of the overlay is the same whether the 2 lines of code are commented out or not. i.e. the border of the overlay rectangle is opaque, but the fill is transparent. The only difference is that there is no flickering when the lines are commented out.

Hi, steve

Thank you for this pointed out. Yes, it does. I didn’t notice that.
Keep it, and call Draw in OnLeftUp. This will reset the bitmap.