开发者

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,


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜