# -*- coding: utf8 -*-
import wx
def load(event):
file = open(filename.GetValue())
contents.SetValue(file.read().decode('utf8'))
file.close()
def save(event):
file = open(filename.GetValue(),'w')
file.write(contents.GetValue().encode('utf8'))
file.close()
app = wx.App()
win = wx.Frame(None, title="Simple Editor", size=(410, 335))
bkg = wx.Panel(win)
loadButton = wx.Button(bkg, label='Open')
loadButton.Bind(wx.EVT_BUTTON, load)
saveButton = wx.Button(bkg, label='Save')
saveButton.Bind(wx.EVT_BUTTON, save)
filename = wx.TextCtrl(bkg)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)
hbox = wx.BoxSizer()
hbox.Add(filename, proportion=1, flag=wx.EXPAND)
hbox.Add(loadButton, proportion=0, border=5)
hbox.Add(saveButton, proportion=0, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(contents, proportion=1, flag=wx.EXPAND | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
上述代码一修改好。
看了一系列的blog之后,终于发现无法进行中文的保存的原因是GetValue 函数的使用应该表明其encode方式(如'utf-8'),而对于SetValue函数也是同理!
而在脚本开头的# -*- coding: utf8 -*-也是必要的。