views:

26

answers:

2

I haven't had to do any GUI programming in a long time, so I might be being obtuse here, so please bear with me if this is a stupid question. I decided to use wxPython for a small hobby project, and I'm having trouble changing the background colour of the main window. I'm using Python 2.6.2 and wxPython 2.8.11.0 on Snow Leopard. Can anyone tell me what I'm doing wrong here? Or have I stumbled upon a bug of some sort? Here's a small sample that demonstrates the problem...

from wx import * 

class MainFrame(Frame):
    def __init__(self, parent, title):
        Frame.__init__(self, parent, title=title)

        self.Maximize()
        self.cdatabase = ColourDatabase()
        self.SetBackgroundStyle(BG_STYLE_CUSTOM)
        self.SetOwnBackgroundColour(self.cdatabase.Find('BLACK'))
        self.Show(True)
        self.ClearBackground()


app = App(False)
frame = MainFrame(None, 'a title')
app.MainLoop()
+1  A: 

Your call to self.SetBackgroundStyle(BG_STYLE_CUSTOM) seems to be causing trouble on my system, and also you don't need the line for self.cdatabase = ColourDatabase() at all in my tests. This code works on my side of things:

from wx import * 

class MainFrame(Frame):
    def __init__(self, parent, title):
        Frame.__init__(self, parent, title=title)
        self.Maximize()
        self.SetOwnBackgroundColour('Black')
        self.Show(True)


app = App(False)
frame = MainFrame(None, 'a title')
app.MainLoop()
g.d.d.c
The `BG_STYLE_CUSTOM` was something I tried when the obvious approach didn't work. I just removed it again and tried it, it still doesn't work. It does however work if I add a panel that fills up the Frame and make the panel's background black. Strange...
Chinmay Kanchi
@Chinmay Kanchi - The need for a panel may be platform specific. I'm on XP 64-bit. I do remember reading in various places that you need a Panel for consistency across platforms, so maybe that explains some of the discrepancy.
g.d.d.c
Fair enough. Ah the joys of cross-platform toolkits...
Chinmay Kanchi
+1  A: 

The thing to remember with wxPython is that for the most part, it wraps the native widgets of the platform it is on. So if the frame on Linux doesn't support changing its background color, than you can't do it with just the frame. (Note: I don't know which platforms wx.Frame supports bg color changing)

The wx.Panel should always be included for consistent look and feel as well as getting tabbing to work correctly on the child widgets. If you want to be able to completely control every aspect of your application, you would need to use a different toolkit. By the way, many of the core controls in wxPython have generic counterparts that were written in pure python and can be hacked to do stuff that native widgets cannot.

Mike Driscoll