2013-03-27 92 views
1

說我使用一個循環,像這樣創造6文本控制字段期間創建TextCtrl值:我如何在wxPython循環

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

我怎樣才能創建之後更新這些?

回答

1

你不能。 您必須以某種方式保存對象(p.e.對象列表)或爲每個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] 
+0

我可以在使用循環時保存對象嗎?恩。使用項目作爲對象名稱? – pedram 2013-03-27 17:17:28

+0

請參閱編輯 – joaquin 2013-03-27 17:35:53

1

如果你想稍後再引用它們,你不能創建TextCtrl。存儲它們。

一種方式是通過使用settatr()將它們添加到該對象,以及可選的前綴爲額外的信息(我用「_text」)

http://docs.python.org/2/library/functions.html#setattr

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

,以後你可以訪問它並通過使用例如更改值:

self._text_bid.SetValue('Bid v2.0') 
2

有一個更簡單的方法。您可以將一個唯一的名稱傳遞給文本控件並使用它來更新它們。我提供了一個簡單的例子,我更新了第一對夫婦的如下控制:

import wx 

######################################################################## 
class MyPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

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

     btn = wx.Button(self, label="Update") 
     btn.Bind(wx.EVT_BUTTON, self.updateTextCtrls) 
     sizer.Add(btn) 

     self.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def updateTextCtrls(self, event): 
     """""" 
     txtCtrls = [widget for widget in self.GetChildren() if isinstance(widget, wx.TextCtrl)] 
     for ctrl in txtCtrls: 
      if ctrl.GetName() == "bid": 
       ctrl.SetValue("$100") 
      elif ctrl.GetName() == "ask": 
       ctrl.SetValue("$500") 


######################################################################## 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="TextCtrl Tutorial") 
     panel = MyPanel(self) 
     self.Show() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    app.MainLoop() 

創建的文本控件引用的唯一方法是使用ikaros45提到的SETATTR方法。

+0

如果您知道您的小部件ID,也可以使用文本控件的GetId()方法,但我發現GetName更容易。 – 2013-03-27 19:02:27

2

一個非常簡單的方法是將ctrl存儲在字典中。 我修改了Mike的代碼來使用字典,而不是看下面。

import wx 

######################################################################## 
class MyPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     ticker_items = ['bid', 'ask', 'open', 'close', 'high', 'low'] 
     self.ticker_ctrls = {} 
     for item in ticker_items: 
      ctrl = wx.TextCtrl(self, value=item, size=(-1, -1), name=item) 
      sizer.Add(ctrl) 
      self.ticker_ctrls[item] = ctrl 

     btn = wx.Button(self, label="Update") 
     btn.Bind(wx.EVT_BUTTON, self.updateTextCtrls) 
     sizer.Add(btn) 

     self.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def updateTextCtrls(self, event): 
     """""" 
     self.ticker_ctrls['bid'].SetValue('$100') 
     self.ticker_ctrls['ask'].SetValue('$500') 


######################################################################## 
class MainFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="TextCtrl Tutorial") 
     panel = MyPanel(self) 
     self.Show() 

if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MainFrame() 
    app.MainLoop()