我正在尝试对 wxFormBuilder 接口进行编程,以将 wxListBox 中的文本写入文本文件。我当前的代码:
def clickSave (self, parent):
dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", "*.*", wx.SAVE)
if dialog.ShowModal() == wx.ID_OK:
fn = dialog.GetPath()
fh = open(fn, "w")
for i in range(self.m_listBox4.GetCount()):
listBox = self.m_listBox4.GetString(i) + "\n"
fh.write(listBox)
fh.close()
目前,此代码仅保存列表框中的最后一个条目,而不是所有条目。我也无法将文本文件中的列表导入 wxListBox。使用我拥有的代码,我收到“TypeError:需要字符串或 Unicode 类型”错误:
def clickOpen(self, event):
dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", "*.*", wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
stockinfo = []
fn = dialog.GetPath()
fh = open(fn, "r")
csv_fh = csv.reader(fh)
for row in csv_fh:
stockinfo.append(row)
fh.close()
for rows in stockinfo:
self.m_listBox4.Append(rows)