wxpython controlling a widget with the id
hey :) ok im looking for a way to create a large number of panels in wxpython and append a hold to them in a list but am not sure how best to do this. for example for i in list: wx.P开发者_StackOverflowanel(self, -1, pos, size) #create the panel
somehow store a hold to it e.g
anotherlist.append(a) #where a is the hold to the panel when i say hold i mean say the variable name is x, so x = wx.Panel. i would call x the hold cos x can be used for any manipulation of the widget, e.g x.SetPosition etc.. i was thinking maybe using a class something(wx.Panel) that creates the panel and saves the id of the panel.. problem is having the id i have no idea how to access the widget. say the panels id is -206. how do i do something like widgetid(-206).SetBackgroundColour("RED")
Some people solve these sorts of things by creating the ids at the beginning of the file:
panelOneId = wx.NewId()
panelTwoId = wx.NewId()
And then doing something like myPanel = wx.FindWindowById(panelOneId). Of course, if all you're doing is setting panel attributes, it might just behoove you to create a helper method like this:
#----------------------------------------------------------------------
def createPanel(self, sizer, id, bg):
""""""
panel = wx.Panel(self, id=id)
panel.SetBackgroundColour(bg)
sizer.Add(panel)
You can also use wx.FindWindowByName, if you've given the panels unique name parameters.
A simple solution is to use a dictionary to map ids to panels
panels = {}
for i in range(100):
id = wx.NewId()
panels[id] = wx.Panel(parent, id, ...)
You then have access to a list of ids (.keys()
), a list of panels (.values()
) and a mapping from id to panel.
精彩评论