Hey all,

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

text2 = wx.StaticText(self, -1, "text2", size=(30,-1))

text3 = wx.StaticText(self, -1, "text3", size=(50,-1))

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.

Another way to to add the controls to a list.

# initialize controls
text_ctrls = []
for x in xrange(1,4):
    text_ctrls.append(wx.StaticText(self, -1, "Text%d" %x, size=( (x-1)*20+10 ,-1)))

# set attributes
for x in xrange(3):
    text_ctrls[x].SetBackgroundColour(color[x])  # color can be initialized in a list somewhere

Why don't use regular, readable function:

def mystatic(self,label,size,font):
    text = wx.StaticText(self, -1, label, size=(size,-1))
    return text

Wow, thanks tonyjv, that never even occurred to me! I'll have to give that a try, indeed it looks a lot more readable and simple.

cghtkh, I'll give that method a try too, looks promising too.