我创建了一个带有按钮的 wxframe,单击该按钮会弹出一个子框架,该子框架生成文本控件,其中的数据存储在名为“samples”的字典中,如下所示。子框架的想法是它是一个用于编辑数据的编辑窗口。
我的问题是我不知道如何从生成的文本控件中检索数据。我只能从最后生成的文本控件中获取数据。
这是我创建的代码的简化版本:
import wx
class MyFrame1(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame1.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
self.frame=wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.f3 = wx.Frame(None,-1,'Edit Tags')
self.editTag = wx.Button(self.panel_1, -1, "Edit Tags", size=(100, 50))
self.editTag.Bind(wx.EVT_BUTTON, self.editTags)
self.Bind( wx.EVT_CLOSE, self.CloseAll)
# end wxGlade
#-------------------------------------------------------------------------------
def editTags(self, event): # wxGlade: MyFrame.<event_handler>
samples={
"Face1":"(100,100)", \
"Face2":"(50,40)", \
"Car":"(500,230)", \
"Wallclock":"(120,230)"
}
self.f3.okButton = wx.Button(self.f3, label="OK")
self.f3.okButton.Bind(wx.EVT_BUTTON, self.onOK)
self.f3.okButton.SetPosition((40,200))
spacer=0
for k, v in sorted(samples.iteritems()):
print "K: ", k
spacer=spacer+40
print "V: ", v
print
self.f3.new_TextControl = wx.TextCtrl(self.f3, -1, value=k)
self.f3.new_TextControl.SetPosition((40,spacer))
print
self.f3.Show()
#-------------------------------------------------------------------------------
def onOK(self, event): # wxGlade: MyFrame.<event_handler>
print self.f3.new_TextControl.GetValue()
print self.f3.new_TextControl.GetValue()
print "OK clicked"
self.f3.Hide()
#-------------------------------------------------------------------------------
def CloseAll(self, event):
self.DestroyChildren() # First destroy child frames
self.Destroy() # Then destroy the parent frame
self.f3.Close()
self.Close()
#-------------------------------------------------------------------------------
# end of class MyFrame1
if __name__ == "__main__":
app = wx.PySimpleApp(0)
frame_1 = MyFrame1(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
也欢迎任何替代解决方案
我正在使用 python 2.7.2。和 wxpython