static text and transparent background

1,351 views
Skip to first unread message

Michele Petrazzo

unread,
Sep 11, 2006, 7:45:29 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Hi,
I'm trying to create a program that has an image like background and
some static text widgets into it. I try with OnPaint event for draw the
bg image (and this work well), but when I put a statictext into it, I
see that the background are that of the static (so gray), and not of my
image, so I tried with OnPaint / EraseBackground methods of the static,
but all without fortune...
In the better case I can draw myself the text of the StaticText into the
PaintDc, but the background are that of the window under! (I think that
wx don't paint the rect of the widget)

What can I do? There is a already done widget that do this?

Thanks,
Michele

Gavana, Andrea

unread,
Sep 11, 2006, 8:08:35 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Hello Michele,

> In the better case I can draw myself the text of the
> StaticText into the PaintDc, but the background are that of
> the window under! (I think that wx don't paint the rect of the widget)

That is be the way I would go for it... Does the look changes if you add:

dc.SetBackgroundMode(wx.TRANSPARENT)

Before drawing the text in the OnPaint()?

Andrea.

_________________________________________
Andrea Gavana (gav...@kpo.kz)
Reservoir Engineer
KPDL
4, Millbank
SW1P 3JA London

Direct Tel: +44 (0) 20 717 08936
Mobile Tel: +44 (0) 77 487 70534
Fax: +44 (0) 20 717 08900
Web: http://xoomer.virgilio.it/infinity77
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Gavana, Andrea

unread,
Sep 11, 2006, 8:16:13 AM9/11/06
to wxPytho...@lists.wxwidgets.org
> That is be the way I would go for it... Does the look changes
> if you add:

Sorry for the spaghetti english, I must be blind or someone must have put some LSD in my lunch today :-D

Michele Petrazzo

unread,
Sep 11, 2006, 9:22:35 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Gavana, Andrea wrote:
> Hello Michele,
>
>> In the better case I can draw myself the text of the
>> StaticText into the PaintDc, but the background are that of
>> the window under! (I think that wx don't paint the rect of the widget)
>
> That is be the way I would go for it... Does the look changes if you add:
>
> dc.SetBackgroundMode(wx.TRANSPARENT)
>
> Before drawing the text in the OnPaint()?
>

Already tried, but the same. If you want I can create a small app for
show the problem.

> Andrea.
>

Ciao, :)
Michele

Gavana, Andrea

unread,
Sep 11, 2006, 9:29:12 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Hello Michele,

> Already tried, but the same.

Uhm, this seems strange to me, it is exactly the same thing I am doing with CustomTreeCtrl, that supports a background image. The text I draw has a transparent background. Well, I am not using dc.DrawText() but dc.DrawLabel()... However I don't see such big differences between the 2 methods.

> If you want I can create a small
> app for show the problem.

Yes, that would be the best option... Just to let us play with something :-D

Ciao!

Kent Quirk

unread,
Sep 11, 2006, 9:45:26 AM9/11/06
to wxPytho...@lists.wxwidgets.org

A co-worker and I spent a couple of days earlier this summer
experimenting with trying to create various transparent controls in an
app. We got a few things to work sometimes, but then found that the
behavior varied based on the video system in the computer (different
video cards on the same operating system) so we dropped it. Basically,
if you don't want your app to look like a platform-native application,
you have to draw EVERYTHING yourself. So far, we've ended up creating
listbox, checkbox, and static text controls on top of a wxPanel to get
the appearance and behavior we want (and even there we've dropped the
idea of transparency).

The frustrating part is that it's not possible to get sufficient
information on appearance from most wxControls to render them yourself.
You have to recreate all the behavior logic as well. This may be the
fault of the underlying platform APIs -- but it would be nice to be able
to have (for example) a wx.ListBox that does everything except paint
itself. It's not very much fun to create a working ListBox from scratch.

Kent


Gavana, Andrea

unread,
Sep 11, 2006, 9:54:59 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Hello Kent,

> The frustrating part is that it's not possible to get
> sufficient information on appearance from most wxControls to
> render them yourself.
> You have to recreate all the behavior logic as well. This may
> be the fault of the underlying platform APIs -- but it would
> be nice to be able to have (for example) a wx.ListBox that
> does everything except paint itself. It's not very much fun
> to create a working ListBox from scratch.

This goes in favour of a "skinnable" wxPython (wxWidgets?!?!), and it is somewhat contradictory wrt the underlying philosophy of wx*. However, I can perfectly understand what you feel, because every time I needed a new/different control, I had to redo everything from scratch, which is a little bit frustrating. If you need an existing control with different appearance, the best choice you have (a part of redoing *all* the job from scratch) is to look if a generic/common implementation of that control exists in the wxWidgets source tree. At least, you can translate the C++ code to Python instead of re-thinking the event/paint/size strategies from scratch. Otherwise, it is a PITA of trial and error until you get what you need :-D

Gavana, Andrea

unread,
Sep 11, 2006, 11:21:13 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Hello Michele,

Try not to use a wx.StaticText: simply draw the text in the OnPaint of your panel, like this:


import wx

class frame(wx.Frame):

def __init__(self):

wx.Frame.__init__(self, None, style= wx.DEFAULT_FRAME_STYLE)

self._p = wx.Panel(self)
self.mytext = "A LOT BIG TEXT"

#Event bind
self._p.Bind(wx.EVT_PAINT, self.onPaint)
self._p.Bind(wx.EVT_SIZE, self.OnSize)
self._p.Bind(wx.EVT_ERASE_BACKGROUND, self._DoEraseBG)

#Image
self._bmp = wx.Bitmap("./image.jpg")
self._currentBmpSize = self._bmp.GetSize()
self._p._bmp = self._bmp
#Show
self.Show()

def _DoEraseBG(self, evt):
"""
"""
#print "erase"

def onPaint(self, evt):
print "panint frame"
dc = wx.BufferedPaintDC(self._p)

dc.DrawBitmap(self._bmp, 0,0)

dc.SetBackgroundMode(wx.TRANSPARENT)
dc.SetFont(wx.Font(40, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL))
dc.DrawText(self.mytext, 20, 20)

evt.Skip()

def OnSize(self, evt):
size = self.GetClientSize()
if size != self._currentBmpSize:
img = wx.ImageFromBitmap(self._bmp).Scale(*size)
self._bmp = wx.BitmapFromImage(img)

def main():
app = wx.PySimpleApp()
f = frame()
app.MainLoop()

if __name__ == "__main__":
main()


HTH.

Michele Petrazzo

unread,
Sep 11, 2006, 12:30:12 PM9/11/06
to wxPytho...@lists.wxwidgets.org
Gavana, Andrea wrote:
>> I know that method, but for my app I want, if I can of course, use
>> a wxStaticText. If there isn't this possibility I'll use your!
>
> Have you tried with wx.lib.stattext.GenStaticText or whatever is its
> name?

Just now

> You may have more luck with that, but I have no idea :-D

Still the same.

> Robin will now for sure what you could do to implement your idea,

I hope that!

> I just run out of intelligent/insightful comments ;-)

All the help are welcome, always!

> Andrea.
>

Ciao,
Michele

Gavana, Andrea

unread,
Sep 11, 2006, 11:56:02 AM9/11/06
to wxPytho...@lists.wxwidgets.org

> I know that method, but for my app I want, if I can of
> course, use a wxStaticText. If there isn't this possibility
> I'll use your!

Have you tried with wx.lib.stattext.GenStaticText or whatever is its name? You may have more luck with that, but I have no idea :-D
Robin will now for sure what you could do to implement your idea, I just run out of intelligent/insightful comments ;-)

Michele Petrazzo

unread,
Sep 11, 2006, 11:46:26 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Gavana, Andrea wrote:
> Hello Michele,
>
> Try not to use a wx.StaticText: simply draw the text in the OnPaint of your panel, like this:
>

You are clever :)


I know that method, but for my app I want, if I can of course, use a
wxStaticText. If there isn't this possibility I'll use your!

Thanks,
Michele

Michele Petrazzo

unread,
Sep 11, 2006, 11:05:33 AM9/11/06
to wxPytho...@lists.wxwidgets.org
Gavana, Andrea wrote:
>> If you want I can create a small app for show the problem.
>
> Yes, that would be the best option... Just to let us play with
> something :-D

The code is attached.
Put an image (image.jpg according to the code) on the same directory and
execute. You'll see the drawn text on the top left, but the background
are that of your above window (or the desktop). Try to maximize and
reduce the app, and you'll see the problem!

P.s. I see this problem only on win... On linux all work!

>
> Ciao!
>
> Andrea.
>

Michele

draw_example.py

Lanier, Paul

unread,
Sep 11, 2006, 1:16:46 PM9/11/06
to wxPytho...@lists.wxwidgets.org
It seemed to me that the Window is automatically getting the
wx.CLIP_CHILDREN style. After a little digging I found that this is
true since 2.5.4 and that
wx.SystemOptions.SetOptionInt('msw.window.no-clip-children', 1)
will get rid of that behavior. So your example works after I add that
line (I put it right after "app = PySimpleApp()")

Once you've set that option, you'll probably want to add
wx.CLIP_CHILDREN to the styles of any windows where you don't need this
transparent effect.

-Paul

-----Original Message-----
From: Michele Petrazzo [mailto:michele....@unipex.it]
Sent: Monday, September 11, 2006 12:30 PM
To: wxPytho...@lists.wxwidgets.org
Subject: Re: [wxPython-users] static text and transparent background

Gavana, Andrea wrote:
>> I know that method, but for my app I want, if I can of course, use a
>> wxStaticText. If there isn't this possibility I'll use your!
>

> Have you tried with wx.lib.stattext.GenStaticText or whatever is its
> name?

Just now

> You may have more luck with that, but I have no idea :-D

Still the same.

> Robin will now for sure what you could do to implement your idea,

I hope that!

> I just run out of intelligent/insightful comments ;-)

All the help are welcome, always!

> Andrea.
>

Ciao,
Michele

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-user...@lists.wxwidgets.org
For additional commands, e-mail: wxPython-...@lists.wxwidgets.org

Robin Dunn

unread,
Sep 11, 2006, 3:45:37 PM9/11/06
to wxPytho...@lists.wxwidgets.org
Michele Petrazzo wrote:
>
> P.s. I see this problem only on win... On linux all work!

Because on Linux the wx.StaticText does internally what Andrea is
suggesting you do, it draws the text directly on the parent.

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


Robin Dunn

unread,
Sep 11, 2006, 3:46:25 PM9/11/06
to wxPytho...@lists.wxwidgets.org
Gavana, Andrea wrote:
>> I know that method, but for my app I want, if I can of course, use
>> a wxStaticText. If there isn't this possibility I'll use your!
>
> Have you tried with wx.lib.stattext.GenStaticText or whatever is its
> name? You may have more luck with that, but I have no idea :-D

Probably not, since it still has a real widget, and it will default its
background to a solid colour.

> Robin
> will now for sure what you could do to implement your idea, I just
> run out of intelligent/insightful comments ;-)

If you really want to have a static text widget then a good generic
approach is you can draw its background the same as the parent, just
calculate the portion of the image that should be shown where the static
text is located, and draw that portion on the background. You'll
probably want to derive a class from wx.lib.stattext.GenStaticText to do
this.

Reply all
Reply to author
Forward
0 new messages