views:

369

answers:

1

I am trying to make a subclass of wx.StaticText that has no background. I tried setting the background's alpha component with SetBackgroundColor, or using SetTransparent on various objects with no luck, but maybe it is only because I am a noobie :)

Anyway, I ended up subclassing wx.StaticText, modifying the OnPaint method so that no background is painted, and binding EVT_PAINT to this new method.

It works very well most of the time (when the application starts, or when it is repainted after being hidden by another window), but whenever I use the SetLabel method on an instance of my subclass of StaticText, it is not my OnPaint method that is called to refresh the window, and I end up with the default gray background.

Below is a "small" working example. It first shows the text with no background, waits 1.5 sec, and then changes the label of the text, which also paints a background (and does not call my homemade painting method).

import wx,time

class MyStaticText(wx.StaticText):
    def __init__(self,parent,id,label,
                 pos=wx.DefaultPosition,
                 size=wx.DefaultSize,
                 style=0):
        wx.StaticText.__init__(self,parent,id,label,pos,size,style)
        self.Bind(wx.EVT_PAINT,self.OnPaint)

    def OnPaint(self,event):
        dc = wx.PaintDC(self)    
        dc.DrawText('Test 3', 40, 0)

class MyFrame(wx.Frame):
    def __init__(self,width=150,height=80):
        wx.Frame.__init__(self, None, size=(width, height))
        self.test_text = MyStaticText(self, -1, 'Test 1',
                             size=wx.Size(width, height))
        self.Show()
        self.Timer = wx.Timer(self,-1)
        self.Timer.Start(1500)
        self.Bind(wx.EVT_TIMER,self.OnTimer)

    def OnTimer(self,event):
        self.test_text.SetLabel('Test 2')

app = wx.App()
MyFrame()
app.MainLoop()

I guess that I am just missing something, but my understanding of wxpython is not good enough to figure out what. It would be greatly appreciated if someone could enlighten me :) And if there is a simpler way to have a text with no background in wxpython, it would be great as well.

Thank you in advance!

+1  A: 

I am afraid that getting reliable results involves making your own custom widget or using someone's implementation. See for example: http://archives.devshed.com/forums/python-122/static-text-and-transparent-background-1975068.html The problem is that StaticText is a very platform-dependent thing and can be customised only in some limited ways.

Vladimir Ignatov
Thank you for your answer. I ended up going with pyQt4 instead of wxpython, facing a few problems as well but eventually getting my application to do what it was supposed to do.
Lay