Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to Set Background Color with wx.DC

Right now, I am setting the background colour like this,

dc.DrawRectangle(0,0,width,height)

Do you know a better way to set the background color?

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

like image 644
user1513192 Avatar asked Aug 01 '12 20:08

user1513192


2 Answers

If you're already painting on a wx.DC to draw the rest of the window's content then the best way is to set the background brush, and then clear the DC. Something like this:

def OnPaint(self, event):
    dc= wx.PaintDC(self)
    dc.SetBackground(wx.Brush(someColour))
    dc.Clear()
    # other drawing stuff...

If you are setting the colour with the window's SetBackgroundColour method, then you can use that instead of some fixed colour value, like this:

def OnPaint(self, event):
    dc= wx.PaintDC(self)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    # other drawing stuff...
like image 185
RobinDunn Avatar answered Sep 28 '22 04:09

RobinDunn


The normal way, which works for most windows, including buttons and other widgets is to call wxWindow::SetBackgroundColour()

http://docs.wxwidgets.org/2.8/wx_wxwindow.html#wxwindowsetbackgroundcolour

The neat thing is that if you call this on the topmost parent, all the children will automatically inherit the same background colour. See the link for how exactly this works.

like image 35
ravenspoint Avatar answered Sep 28 '22 05:09

ravenspoint