简体   繁体   中英

wxPython SetBackgroundColour not working on OS X

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()

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM