下面是针对上次窗口,添加相应的事件。
组件要绑定一个事件函数,才能让组件起作用。如openbutton是一个按钮组件,openfile是一个函数,EVT_BUTTON是一个事件,这一可以通过Bind,把组件和事件函数联系在一起,即openbutton.Bind(EVT_BUTTON,openfile)。
代码如下:
#coding:utf8
#利用布局管理器,管理窗口
import wx
app = wx.App()
win = wx.Frame(None, title = "ahuang1900", size = (410,340)) #创建窗口
bkg = wx.Panel(win) #创建画布
def openfile(evt): #事件函数
filepath = filename.GetValue() #获取值
fopen = open(filepath) #打开文件
contents.SetValue(fopen.read()) #设置值
fopen.close()
def savefile(evt):
filepath = filename.GetValue()
fopen = open(filepath,'w')
fopen.write(contents.GetValue())
fopen.close()
#基于画布来创建主件,这样主件可以置于画布上了
openbutton = wx.Button(bkg, label = "open") #创建主件1
savebutton = wx.Button(bkg, label = "save") #创建主件2
filename = wx.TextCtrl(bkg) #创建主件3
contents = wx.TextCtrl(bkg, style = wx.TE_MULTILINE | wx.HSCROLL) #创建主件4
openbutton.Bind(wx.EVT_BUTTON, openfile) #主件ope