Jeff
Jeff

Reputation: 7210

wxPython .SetFocus() on ID

I'm just wondering if there is a way of setting the focus of say a wx.TextCtrl() based on its ID rather than its name.

Usually you would do something like...

text = wx.TextCtrl(self, 100, '')
text.SetFocus()

and then the focus is set on text. However I'm going to have an undetermined amount of wx.TextCtrls and I'll have a loop to make them, all with different IDs. I'm wondering if their is a way of doing it like this?

'id#'.SetFocus() #Set focus to TextCtrl with id = id#

I see a way of doing it with dictionaries, but I'm assuming there is a better way of doing this.

Thanks,

Upvotes: 1

Views: 11857

Answers (1)

FogleBird
FogleBird

Reputation: 76762

I never use wx ID's, because there are better ways. I'd recommend doing something like this:

self.controls = []
for i in range(100):
    control = wx.TextCtrl(self, -1, '')
    self.controls.append(control)
    sizer.Add(control)

...

self.controls[12].SetFocus()

Or you can use a dictionary when it makes more sense than a list, depending on how you need to look them up.

If you're hard set on using the ID, you can try something like this:

self.FindWindowById(id, self).SetFocus()

Upvotes: 7

Related Questions