<pre name="code" class="plain">看到python GUI那一章,然后就对着书本撸下来,其实也就是书本上的代码。重新熟悉了下。
#my_first_python_GUI_prog#SimpleEditor
import wx
def load(event):
file = open(file_name.GetValue())
contents.SetValue(file.read)
file.close()
def save(event):
file = open(file_name.GetValue(), 'w')
file.write(contents.GetValue())
file.close()
app = wx.App()
win = wx.Frame(None, title = "Simple Editor", size = (400, 600))
bkg = wx.Panel(win)
load_button = wx.Button(bkg, label = "Open")
load_button.Bind(wx.EVT_BUTTON, load)
save_button = wx.Button(bkg, label = "Save")
save_button.Bind(wx.EVT_BUTTON, save)
file_name = wx.TextCtrl(bkg)
contents = wx.TextCtrl(bkg, style = wx.TE_MULTILINE | wx.HSCROLL)
#default = HORIZONTAL
hbox = wx.BoxSizer()
hbox.Add(file_name, proportion = 1, flag = wx.EXPAND)
hbox.Add(load_button, proportion = 0, flag = wx.LEFT, border = 5)
hbox.Add(save_button, proportion = 0, flag = wx.LEFT, 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.LEFT | wx.BOTTOM | wx.RIGHT, border = 5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
不过,在运行时出现了问题。在Save时很正常,但是Open时,却给窝TypeError:String or Unicode type required。窝用的是unicode版本的。不知道咋回事。