Skip to main content
 首页 » 编程设计

python之如何获取在 wxpython 循环期间创建的 TextCtrl 的值

2025年05月04日14Renyi-Fan

假设我正在使用这样的循环创建 6 个文本控制字段:

ticker_items = ['bid', 'ask', 'open', 'close', 'high', 'low'] 
for item in ticker_items: 
    wx.TextCtrl(self.panel, -1, value=item, size=(-1, -1)) 

创建后如何更新这些内容?

请您参考如下方法:

你不能。 您要么必须以某种方式保存对象(例如对象列表),要么为每个 TextCtrl 提供不同的 ID。

例如:

ticker_items = [('bid', 1000), ('ask', 1001), 
                ('open', 1002), ('close', 1003), 
                ('high', 1004), ('low', 1005)] 
 
for item, id in ticker_items: 
    wx.TextCtrl(self.panel, id=id, value=item, size=(-1, -1)) 

然后就可以使用了

my_textctrl = self.panel.FindWindowById(id_of_my_ctrl) 

获取特定的控件

或者,使用列表:

ticker_items = ['bid', 'ask', 'open', 'close', 'high', 'low'] 
self.my_controls = [] 
for item in ticker_items: 
    text_control = wx.TextCtrl(self.panel, -1, value=item, size=(-1, -1)) 
    self.my_controls.append(text_control) 

你检索你的文本号码 0 用

 my_textctrl = self.my_controls[0]