Hopefully this will help someone with a problem I encountered. For a few days now I was looking for a way to concisely set the attributes to numerous controls without duplicating attribute code again and again. For example, let's say I have 3 static text controls and I would like to change the background and foreground color and font style on each. I would need to do something like:
text1 = wx.StaticText(self, -1, "text1", size=(10,-1)) text1.SetBackgroundColour('#444444') text1.SetForegroundColour('#ffffff') text1.SetFont(text1) text2 = wx.StaticText(self, -1, "text2", size=(30,-1)) text2.SetBackgroundColour('#444444') text2.SetForegroundColour('#ffffff') text2.SetFont(text1) text3 = wx.StaticText(self, -1, "text3", size=(50,-1)) text3.SetBackgroundColour('#444444') text3.SetForegroundColour('#ffffff') text3.SetFont(text1)
Tedious right? Now imagine doing this for many many text controls, buttons, etc etc.
It sure would be nice to just use, say a function, to set the attributes of a particular control. So here's what I devised:
# Set the function that defines all of the attributes changes to the passed controls def setControlAttributes(name, param): if param&1==1: name.SetBackgroundColour('#444444') if param>>1&1==1: name.SetForegroundColour('#ffffff') if param>>2&1==1: name.SetFont(text_font) text1 = wx.StaticText(self, -1, "text1", size=(10,-1)) setControlAttributes(text1, 1) text2 = wx.StaticText(self, -1, "text2", size=(30,-1)) setControlAttributes(text2, 4) text3 = wx.StaticText(self, -1, "text3", size=(50,-1)) setControlAttributes(text3, 7)
In the above case, text1 will gain attribute 1 properties, text2 will gain attribute 2 properties, and text3 will gain all three attribute properties.
Remember 1 in binary is 001, 4 is 010, and 7 is 111. Attribute 1 corresponds to the first bit, attribute the second bit, etc. You can add any number of attributes as you like (within reason). Hope this helps someone out there.