1

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.Panel(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")

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much mike wx.FindWindowById is exactly what i was looking for! :)
1

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.

1 Comment

it would seem as mike said tho, that wx.FindWindowById() is basically the same and you dont have to create the dictionary anyway as wx.FindWindowById(20) would return the wx.Panel(self, id=20) object... thanks tho answer definitely is usefull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.