我需要在我的编辑器中保存一个文本文件,这是用于保存文件的代码,但代码在Windows 7中不起作用 我的代码是
def OnSaveFile(self, event):
if self.last_name_saved:
try:
file = open(self.last_name_saved, 'w')
text = self.text.GetValue()
file.write(text.encode('utf-8'))
file.close()
self.statusbar.SetStatusText(os.path.basename(self.last_name_saved) + ' saved', 0)
self.SetTitle(os.path.basename(self.last_name_saved))
self.modify = False
self.statusbar.SetStatusText('', 1)
except IOError, error:
dlg = wx.MessageDialog(self, 'Error saving file\n' + str(error))
dlg.ShowModal()
else:
self.OnSaveAsFile(event)
这是我的错误。
Traceback (most recent call last):
File "F:\EZHUTHANI_WIN\ezhuthani\beditor.py", line 301, in OnSaveFile
self.OnSaveAsFile(event)
File "F:\EZHUTHANI_WIN\ezhuthani\beditor.py", line 308, in OnSaveAsFile
if save_dlg.ShowModal() == wx.ID_OK:
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_windows.py", line 711, in ShowModal
return _windows_.Dialog_ShowModal(*args, **kwargs)
这是用于在程序中保存文件的模块
def OnSaveAsFile(self, event):
wcd='All files(*)|*|Editor files (*.ef)|*.ef|'
dir = os.getcwd()
save_dlg = wx.FileDialog(self, message='Save file as...', defaultDir=dir, defaultFile='',
wildcard=wcd, style=wx.SAVE | wx.OVERWRITE_PROMPT)
if save_dlg.ShowModal() == wx.ID_OK:
path = save_dlg.GetPath()
try:
file = open(path, 'w')
text = self.text.GetValue()
file.write(text.encode('utf-8'))
file.close()
self.last_name_saved = os.path.basename(path)
self.statusbar.SetStatusText(self.last_name_saved + ' saved', 0)
self.modify = False
self.statusbar.SetStatusText('', 1)
self.SetTitle(window_title + path)
except IOError, error:
dlg = wx.MessageDialog(self, 'Error saving file\n' + str(error))
dlg.ShowModal()
save_dlg.Destroy()
答案 0 :(得分:0)
检查OnSaveAsFile()函数中的通配符,
将其更改为
wcd='Text files(*.txt)|*.txt|Plain Text files (*.txt)|*.txt'