wx.StaticText size (12KB attachment)

Algirdas Brazas wrote:

I put several lines of text using StaticText for each line. I calculate line positions having in mind font size and adding some space. And there I get some problems - StaticText size is actualy bigger than the text itself.

wx.StaticText is designed for simple labels and such -- not for more complex laing out of text. You can put them together with Sizers, but I think what you really want to do is to draw on your wx.Window or wx.Panel with wx.DC.DrawText() -- you can use wx.DC.GetTextExtents() to see how big each of your text elements are. Be forewarned that wx doesn't provide the tools to do real typesetting, but simple stuff works fine.

See the demo and Wiki for examples of how to use the wx.DC classes.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thank You a lot. At the moment I'have figured out problem with text margins. What
I'we done is more a workaround, but it works :slight_smile:
My problem was that the code:

font=wx.Font(18,wx.DEFAULT,wx.NORMAL,wx.BOLD)
memoryDC = wx.MemoryDC()
memoryDC.SetFont(font)
w,h=memoryDC.GetTextExtent("Test")

returned 29 for the height. In the example I'we used preconfigured size font, but in real app font is set by the user. Anyway, I found out, that if I multiply height by 0.6 I'll get average font height. And when drawing with wx.DC there is no more blank borders covering text, so it is working for the moment.

There is one more question that I google for 3 hourls already. I want to make background transparent. So I use self.SetTransparent(). But then I cannot make drawn text lines opaque. From the other side if I paint frame with transparent color I can see background color. How could I get rid of this background?
Or should I make frame size 0 and use ScreenDC?

Algirdas

···

----- Original Message ----- From: "Christopher Barker" <Chris.Barker@noaa.gov>
To: <wxPython-users@lists.wxwidgets.org>
Sent: Friday, December 28, 2007 7:33 PM
Subject: Re: [wxPython-users] wx.StaticText size (12KB attachment)

Algirdas Brazas wrote:

Yep, I use GetExtents to get line size. And it is so, that this size is bigger than the text itself.
Excuse me for noobie question - when using wx.DC will I get the text as bitmap or will it still be selectable?

Just the image -- a bitmap if you want, or just the screen itself. Any selection or anything you want you'd have to write yourself. But wx.StaticText doesn't provide selectable text either.

Maybe you want the wx.RichTextCtrl, or another solution.

If you give us the "big picture" of what you are trying to do, we can make suggestions as to how you might do it.

-Chris

Algirdas Brazas wrote:

Thank You a lot. At the moment I'have figured out problem with text margins. What
I'we done is more a workaround, but it works :slight_smile:
My problem was that the code:

font=wx.Font(18,wx.DEFAULT,wx.NORMAL,wx.BOLD)
memoryDC = wx.MemoryDC()
memoryDC.SetFont(font)
w,h=memoryDC.GetTextExtent("Test")

returned 29 for the height. In the example I'we used preconfigured size font, but in real app font is set by the user. Anyway, I found out, that if I multiply height by 0.6 I'll get average font height. And when drawing with wx.DC there is no more blank borders covering text, so it is working for the moment.

Be careful with the 0.6, you may end up clipping things too much if different fonts or different platforms are used. Try using GetFullTextExtent and then you'll also get the descent and leading values.

Also, you should select a bitmap into the DC otherwise you'll get errors on other platforms if you try to use it, even for just measuring.

There is one more question that I google for 3 hourls already. I want to make background transparent. So I use self.SetTransparent(). But then I cannot make drawn text lines opaque. From the other side if I paint frame with transparent color I can see background color. How could I get rid of this background?
Or should I make frame size 0 and use ScreenDC?

SetTransparent sets an alpha transparency for the frame including it's full contents. I assume that instead you just want the text to be fully opaque and the remainder to be fully transparent, right? If so then on Windows you can use the empty EVT_ERASE_BACKGROUND trick. Otherwise you want to use a shaped window, which is shown in the demo. Essentially you use a bitmap, fill it with a known background colour, draw your text to the bitmap, set the mask using the known background colour, create a region from the mask and then set the shape of the frame using the region. Oh yeah, you need to draw the bitmap in the EVT_PAINT handler.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!