所以,我在这里有一个小项目,它搜索文件路径(* .db),然后为这些小部件创建一个复选框和一个文本控件。当我运行应用程序时,这部分工作正常:
# Get a count of *.db from the filesystem
numDB = scrubDB(os.getcwd())
# Checkbox (enable, disable for launch)
# textCtrl (for Proxy name in controller)
# database name (based on *.db)
for db in numDB:
check = wx.CheckBox(self, -1, db)
sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
label = wx.StaticText(panel, label="")
sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
name = wx.TextCtrl(panel)
#Set Temp Name
if db.endswith('.db'):
name.Value = db[:-3]
sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=5)
xIndex +=1
#-------------------------------------------------------
sizer.AddGrowableCol(2)
panel.SetSizer(sizer)
这将输出类似:
[ ] test.db test
[ ] test2.db test2
但现在我需要能够访问这些小部件来构建命令。该列表可以是基于scrubDB函数返回的任何数量的.db文件。
我还是Python和wxPython的新手,所以我很感激这里的任何指导。
答案 0 :(得分:0)
您需要动态绑定事件 -
list_checkbox = []
list_textctrl = []
for db in numDB:
check = wx.CheckBox(self, -1, db)
sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
label = wx.StaticText(panel, label="")
sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
name = wx.TextCtrl(panel)
#Set Temp Name
if db.endswith('.db'):
name.Value = db[:-3]
sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=5)
xIndex +=1
# Save references to the widgets created dynamically
list_checkbox += check
list_textctrl += name
# Bind whatever events you want here -
frame.Bind(wx.EVT_CHECKBOX, OnCheck, check)
def OnCheck(*args):
if args[0].IsChecked(): # args[0] holds the reference to the widget which has triggered this event.
id = list_checkbox.index(args[0])
adj_textctrl = list_textctrl[id] # this is your textctrl next to your checkbox
# do whatever you want to do
答案 1 :(得分:0)
这最终起作用了:
for db in numDB:
check = wx.CheckBox(self, -1, db)
sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
label = wx.StaticText(panel, label="")
sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
name = wx.TextCtrl(panel)
#Set Temp Name
if db.endswith('.db'):
name.Value = db[:-3]
sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.TOP, border=5)
xIndex +=1
#-------------------------------------------------------
# Save references to the widgets created dynamically
list_checkboxID.append(check.GetId())
list_checkboxLabel.append(check.GetLabel())
list_txtctrlID.append(name.GetId())
list_txtctrlLabel.append(name.Value)
#Bind whatever events you want here -
check.Bind(wx.EVT_CHECKBOX, self.OnCheck, check)
def OnCheck(self, event):
for item in range(len(list_checkboxID)):
print "Checkbox " + str(item) + ":\t\t\tID:" + str(list_checkboxID[item]) + "\tLABEL:" + list_checkboxLabel[item]
print "Text Control " + str(item) + ":\t\tID:" + str(list_txtctrlID[item]) + "\tLABEL:" + list_txtctrlLabel[item]
我完全意识到可能有一种更聪明的方法来管理元组(我在尝试解决这个问题的过程中学到了很多东西。)