wxTextCtrl doesn't show background as expected on a notebook panel

Hi, everyone,

On Windows XP, wxPython 2.8.3.0, I set the background of a wxTextCtrl
to that of a wxStaticText on the same nootbook panel. Still, they show
different background colors.

In the test code below, the 'label' and 'text' objects both are set to
the same background color, but they are different to the eye. What's
happening here?

When I put the same two objects onto a panel as a direct child of the
frame, they have the same background color. So it seems that the
notebook widget is playing some strange game here. Can anyone give me
an hint and tell me how to set the background color of the TextCtrl to
that of the notebook panel, so that the TextCtrl will appear like a
StaticText?

Best Regards,

···

--
Hong Yuan

大管家网上建材超市
装修装潢建材一站式购物
http://www.homemaster.cn

import wx
class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title,
                          pos=(150, 150), size=(350, 300))

        notebook = wx.Notebook(self)
        panel = wx.Panel(notebook)

        label = wx.StaticText(panel, -1, "Hello World!")
        text = wx.TextCtrl(panel, -1, "Hello World!",
style=wx.TE_READONLY|wx.NO_BORDER|wx.TRANSPARENT_WINDOW)
        text.SetBackgroundColour(label.GetBackgroundColour())
        assert text.GetBackgroundColour() == label.GetBackgroundColour()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(label, 0, wx.ALL, 10)
        sizer.Add(text, 0, wx.ALL, 10)
        panel.SetSizer(sizer)
        panel.Layout()
        notebook.AddPage(panel, "Page1")

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, "Test")
        self.SetTopWindow(frame)
        frame.Show(True)
        return True

app = MyApp(redirect=False)
app.MainLoop()

I looked in the docs here:

http://www.wxpython.org/docs/api/wx.TextCtrl-class.html

and a TextCtrl doesn't have a SetBackgroundColour() method.

Hi,

···

On 4/27/07, 7stud wrote:

I looked in the docs here:

wxPython API Documentation — wxPython Phoenix 4.2.2 documentation

and a TextCtrl doesn't have a SetBackgroundColour() method.

Of course it does. wx.TextCtrl derives from wx.Window, and
SetBackgroundColour is a wx.Window property. As far as I know, every
widget in wx has a SetBackgroundColour method.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

Hi Yuan,

···

On 4/27/07, Yuan HOng wrote:

When I put the same two objects onto a panel as a direct child of the
frame, they have the same background color. So it seems that the
notebook widget is playing some strange game here. Can anyone give me
an hint and tell me how to set the background color of the TextCtrl to
that of the notebook panel, so that the TextCtrl will appear like a
StaticText?

If you just remove the SetBackgroundColour call, does it work? Here it
seems that the StaticText and the TextCtrl have the same background
colour if you don't use the SetBackgroundColour instruction.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

Hi Yuan,

> Hi Yuan,
>
> If you just remove the SetBackgroundColour call, does it work? Here it
> seems that the StaticText and the TextCtrl have the same background
> colour if you don't use the SetBackgroundColour instruction.
>

When SetBackgroundColour is removed for TextCtrl, it gets the defaut
system background, which is white in my case (Windows XP SP2).

I also tried to set the background color of the TextCtrl to that of
its parent panel with:

       text.SetBackgroundColour(panel.GetBackgroundColour())

This also doesn't make the background of the TextCtrl identical to the
notebook panel.

It seems that in a notebook, a penel says it has one background
colour, but actually it has another. And somehow a StaticText control
can correctly adapt itself to use the actual background, while a
TextCtrl can not.

Very perplexing indeed!

By the way, which platform are you using?

I am on Windows XP SP2, wxPython 2.8.3, Python 2.5. If I remove the
SetBackgroundColour, I get the attached effect. I have no idea if this
is theme-related, but right now I am using the non-Windows-default
Zune theme.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

···

On 4/27/07, Yuan HOng wrote:

On 4/27/07, Andrea Gavana <andrea.gavana@gmail.com> wrote:

Friday, April 27, 2007, 2:42:59 AM, Yuan HOng wrote:

Hi, everyone,

On Windows XP, wxPython 2.8.3.0, I set the background of a wxTextCtrl
to that of a wxStaticText on the same nootbook panel. Still, they show
different background colors.

In the test code below, the 'label' and 'text' objects both are set to
the same background color, but they are different to the eye. What's
happening here?

When I put the same two objects onto a panel as a direct child of the
frame, they have the same background color. So it seems that the
notebook widget is playing some strange game here. Can anyone give me
an hint and tell me how to set the background color of the TextCtrl to
that of the notebook panel, so that the TextCtrl will appear like a
StaticText?

I use to do this:

colour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
text.SetBackgroundColour(colour)

If you want to access all system colour indexes try:

[i for i in dir(wx) if i.startswith("SYS_COLOUR")]

['SYS_COLOUR_3DDKSHADOW', 'SYS_COLOUR_3DFACE',
'SYS_COLOUR_3DHIGHLIGHT', 'SYS_COLOUR_3DHILIGHT',
'SYS_COLOUR_3DLIGHT', 'SYS_COLOUR_3DSHADOW',
'SYS_COLOUR_ACTIVEBORDER', 'SYS_COLOUR_ACTIVECAPTION',
'SYS_COLOUR_APPWORKSPACE', 'SYS_COLOUR_BACKGROUND',
'SYS_COLOUR_BTNFACE', 'SYS_COLOUR_BTNHIGHLIGHT',
'SYS_COLOUR_BTNHILIGHT', 'SYS_COLOUR_BTNSHADOW', 'SYS_COLOUR_BTNTEXT',
'SYS_COLOUR_CAPTIONTEXT', 'SYS_COLOUR_DESKTOP',
'SYS_COLOUR_GRADIENTACTIVECAPTION',
'SYS_COLOUR_GRADIENTINACTIVECAPTION', 'SYS_COLOUR_GRAYTEXT',
'SYS_COLOUR_HIGHLIGHT', 'SYS_COLOUR_HIGHLIGHTTEXT',
'SYS_COLOUR_HOTLIGHT', 'SYS_COLOUR_INACTIVEBORDER',
'SYS_COLOUR_INACTIVECAPTION', 'SYS_COLOUR_INACTIVECAPTIONTEXT',
'SYS_COLOUR_INFOBK', 'SYS_COLOUR_INFOTEXT', 'SYS_COLOUR_LISTBOX',
'SYS_COLOUR_MAX', 'SYS_COLOUR_MENU', 'SYS_COLOUR_MENUBAR',
'SYS_COLOUR_MENUHILIGHT', 'SYS_COLOUR_MENUTEXT',
'SYS_COLOUR_SCROLLBAR', 'SYS_COLOUR_WINDOW', 'SYS_COLOUR_WINDOWFRAME',
'SYS_COLOUR_WINDOWTEXT']

-- tacao

No bits were harmed during the making of this e-mail.

Andrea Gavana wrote:

I am on Windows XP SP2, wxPython 2.8.3, Python 2.5. If I remove the
SetBackgroundColour, I get the attached effect. I have no idea if this
is theme-related, but right now I am using the non-Windows-default
Zune theme.

If you look real close you'll see that the bg of the text ctrl is pure white, but the bg of the panel is not. It is off-white with a slight gradient. On some screens the difference is more noticeable than on others.

···

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

When XP themes are active the background of notebook pages is not a
solid colour, but is actually a gradient. wxMSW has to jump through
some hoops to make it so things like static text use the same gradient
background so they end up looking transparent, and not like your
wx.TextCtrl does. However, since wx knows that wxTextCtrl draws its own
background and that it has its own colour settings, it doesn't hack in
the gradient brush.

That's why wx.StaticText reports having one background while actually
showing another! Thanks for the info.

There is a workaround for you though. The notebook has a
GetThemeBackgroundColour method that you can call to get a solid colour
that is an approximation of the gradient brush, so if you use that for
the background of the textctrl it will be a very close match.

Setting the background to notebook.GetThemeBackgroundColour() do
indeed make the background of the wxTextCtrl almost indistinguishable
from that of the notebook.

Many thanks for all that helped!

···

On 4/28/07, Robin Dunn <robin@alldunn.com> wrote:

--
Hong Yuan

大管家网上建材超市
装修装潢建材一站式购物
http://www.homemaster.cn