I'm trying to get the behaviour of an existing wxPython control and simply draw some graphics over that.
The code below shows my current attempt. If I instantiate a StaticTextUnderline
rather than a wx.StaticText
, it behaves perfectly, with the former acting exactly like the latter. In other words, the exact same text showing up inside the control (a 20-line-by-20-character textual piece of drivel):
import wx
class StaticTextUnderline(wx.StaticText):
def __init__(self, parent, *args):
super(StaticTextUnderline, self).__init__(parent, *args)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, e):
super(StaticTextUnderline, self).Update()
# dc = wx.PaintDC(self)
# pen = wx.Pen(wx.Colour(255,0,0))
# pen.SetWidth(1)
# dc.SetPen(pen)
# dc.DrawLine(0, 0, 50, 50)
The commented code is meant to simply draw a red line over the text after the static text has been painted. Unfortunately, the instant I uncomment the first of those lines, the control no longer renders the text. I suspect getting another device context may be clearing the current content. If I uncomment all of it, I get the red line but that's no good to me if the underlying static text has been cleared somehow.
How can I intercept an arbitrary wxPython
control and just fiddle with the content after it has done its work?
Okay, so it looks like the original thoughts (that the second paint device context may be causing the issue) are correct. After originally trying to figure out how to get one context and use it both for the superclass call and the OnPaint
calls, it turns out that you can do this simply by getting a different type of device context (a client one).
There are warnings in the documentation about using a non-paint context in the paint event (and a paint context outside of a paint event) but I suspect this just has to do with the first context obtained.
So the following code appears to work, rendering both the underlying text and the red line:
def OnPaint(self, e):
super(StaticTextUnderline, self).Update()
dc = wx.ClientDC(self)
pen = wx.Pen(wx.Colour(255,0,0))
pen.SetWidth(1)
dc.SetPen(pen)
dc.DrawLine(0, 0, 50, 50)