I am trying to create a resizable static text control that will respond to
changes in window size by changing its font size. Initially I tried a
wxStaticText control, giving it an OnSize method responding to an
EVT_SIZE event. The OnSize method calculated a new font size, and
attempts to refresh the control. This effort fails, the wxStaticText
control refuses to recognize the change font size. All my attempts
to get it to do so (e.g. pretending to change the label) gave
unexepected results.
I then made a control that actually drew the text. This control worked
the way I expected, but unfortunately, I cannot get it to appear without
some kind of border drawn around it. It would seem to me, that if
a wxStaticText control is a wxControl is a wxWindow, and a wxStaticText
can appear without a border, then I too should be able to create a wxControl
or wxWindow, and have it appear without borders. Thats what object
oriented programming is about, isn't it?
Anyhow, If someone knows how to get a borderless window that I can
draw in, or can show me how to get a wxStaticText control to respond
to font size changes, I would appreciate your expertise.
- Parzival
···
-------------------------------
# This does not work:
class Widget:
def PixPerPoint(self):
return float(self.GetCharHeight())/self.GetFont().GetPointSize()
def SetFontSize(self,pix):
f = self.GetFont()
pt = min(24, max(6,int(pix/self.PixPerPoint()+0.5)))
#wxLogMessage("SetFontSize: pix:%d points:%d" % (pix,pt))
f.SetPointSize(pt)
class StaticText2(wxStaticText,Widget):
def __init__(self, parent, ID, text,
pos = wxDefaultPosition, size = (100,12),
style = 0, name=None):
if not name: name = str(ID)
wxStaticText.__init__(self, parent, ID, text, pos, size=size,
style=style, name=name)
EVT_SIZE(self, self.OnSize)
def OnSize(self,event):
self.SetFontSize(event.GetSize().height)
self.Refresh()
--------------------------------
# This does work, but I can't get rid of the border
AlignTop = 0
AlignMidRise = 1
AlignMidText = 2
AlignBot = 3
AlignLeft = 0
AlignCenter = 4
AlignRight = 8
class StaticText(wxControl,Widget):
def __init__(self, parent, ID, text, align=0,
pos = wxDefaultPosition, size = (100,12),
style = 0, name=None):
self.text = text
self.align = align
if style == 0:
style = wxSTATIC_BORDER
if not name: name = str(ID)
wxControl.__init__(self, parent, ID, pos, size, style,
validator = wxDefaultValidator, name=name)
self.SetFontSize(12)
EVT_SIZE(self, self.OnSize)
EVT_PAINT(self, self.OnPaint)
def OnPaint(self, event):
self.Draw(wxPaintDC(self))
def OnSize(self,event):
self.SetFontSize(event.GetSize().height-2)
pass # wxLogMessage("SetSize: " + str(event.GetSize()))
def Draw(self,dc):
w, h = self.GetClientSizeTuple()
dc.BeginDrawing()
dc.SetFont(self.GetFont())
tw, th, td, tx = self.GetFullTextExtent(self.text)
alignV = self.align & 3
if alignV == AlignTop:
y = 0
elif alignV == AlignMidRise:
y = (h - th + td)/2
elif alignV == AlignMidText:
y = (h - th)/2
else: # alignV == AlignBot:
y = h - th
alignH = self.align & 12
if alignH == AlignLeft:
x = 0
elif alignH == AlignCenter:
x = (w - tw)/2
else: # alignH == AlignRight:
x = w - tw
dc.DrawText(self.text, x, y)
dc.EndDrawing()
_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users