wx 打开和保存文件

创建应用程序对象:

app = wx.App()

进入应用程序管理

app.MainLoop()

创建窗口 window Frame

win = wx.Frame(None)

显示窗口:

win.Show()

在窗口中添加组件

btn = wx.Button(win)

设置组件位置,尺寸

size pos

添加组件

text control :wx.TextCtrl 默认文本框,

设置为文本区

style=wx.TE_MULTILINE | wx.HSCROLL

尺寸器: sizer wx.BoxSizer

 作用:管理添加在其中的组件布局位置

hbox = wx.BoxSizer(wx.HORIZONTAL wx.VERTICAL)

增加panel以利于布局

sizer.add(组件,proportion=flag=border=)


import wx
app = wx.App()
win = wx.Frame(None)
btn = wx.Button(win)
win.Show()
app.MainLoop()

win = wx.Frame(None, title="Simple Editor")
win = wx.Frame(None, title="Simple Editor", size=(410, 335))
loadButton = wx.Button(win, label='Open')
saveButton = wx.Button(win, label='Save')

loadButton = wx.Button(win, label='Open‘,pos=(225, 5), size=(80, 25))
saveButton = wx.Button(win, label='Save‘, pos=(315, 5), size=(80, 25))
filename = wx.TextCtrl(win, pos=(5, 5), size=(210, 25))
contents = wx.TextCtrl(win, pos=(5, 35), size=(390, 260), style=wx.TE_MULTILINE | wx.HSCROLL)

bkg = wx.Panel(win)
loadButton = wx.Button(bkg, label='Open')
saveButton = wx.Button(bkg, label='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, flag=wx.LEFT, border=5)
hbox.Add(saveButton, 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)


事件处理:

组件的Band绑定事件处理函数


def load(event):
    file = open(filename.GetValue())
    contents.SetValue(file.read())
    file.close()

def save(event):
    file = open(filename.GetValue(), 'w')
    file.write(contents.GetValue())
    file.close()

loadButton = wx.Button(bkg, label='Open')
loadButton.Bind(wx.EVT_BUTTON, load)
saveButton = wx.Button(bkg, label='Save')
saveButton.Bind(wx.EVT_BUTTON, save)


你可能感兴趣的:(python)