python wxpython--学习笔记

参考了 Magnus LH 的教程, 司维等翻译,请勿转载

最简单的app:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
app.MainLoop()

创建并显示一个框架:

#! _*_ coding=UTF-8 _*_

import wx
myapp=wx.App()
win=wx.Frame(None)
win.Show()

app.MainLoop()


添加一个按钮:

#! _*_ coding=UTF-8 _*_

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

app.MainLoop()


使用构造函数添加标题、标签等:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
win=wx.Frame(None,title="editor")
loadButton=wx.Button(win,label='open')
saveButton=wx.Button(win,label='save')
win.Show()
app.MainLoop()


使用构造函数提供位置、尺寸等:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
win=wx.Frame(None,title="editor",size=(400,330))
loadButton=wx.Button(win,label='open',pos=(150,5),size=(50,20))
saveButton=wx.Button(win,label='save',pos=(250,5),size=(50,20))
win.Show()
app.MainLoop()


添加文本控件:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
win=wx.Frame(None,title="editor",size=(400,330))
loadButton=wx.Button(win,label='open',pos=(150,5),size=(50,20))
saveButton=wx.Button(win,label='save',pos=(250,5),size=(50,20))
filename=wx.TextCtrl(win,pos=(50,5),size=(100,20))
win.Show()
app.MainLoop()


增加文本区、滚动条:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
win=wx.Frame(None,title="editor",size=(400,330))
win.Show()

loadButton=wx.Button(win,label='open',pos=(150,5),size=(50,20))
saveButton=wx.Button(win,label='save',pos=(250,5),size=(50,20))
filename=wx.TextCtrl(win,pos=(50,5),size=(100,20))
contents=wx.TextCtrl(win,pos=(50,35),size=(400,200),style=wx.TE_MULTILINE|wx.HSCROLL)

app.MainLoop()

智能布局:

#! _*_ coding=UTF-8 _*_
import wx
app=wx.App()
win=wx.Frame(None,title="editor",size=(400,330))
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(wx.HORIZONTAL)       
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.ALL|wx.BOTTOM|wx.RIGHT,border=5)

bkg.SetSizer(vbox)
win.Show()
app.MainLoop()

 

 添加事件处理:

#! _*_ coding=UTF-8 _*_
import wx


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


app=wx.App()
win=wx.Frame(None,title="editor",size=(400,330))
bkg=wx.Panel(win)                   #增加背景组件
loadButton =wx.Button(bkg,label='open')
loadButton.Bind(wx.EVT_BUTTON,load)

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

filename=wx.TextCtrl(bkg)
contents=wx.TextCtrl(bkg,style=wx.TE_MULTILINE|wx.HSCROLL)
hbox=wx.BoxSizer(wx.HORIZONTAL)         #创建尺寸器,并将组件装入
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.ALL|wx.BOTTOM|wx.RIGHT,border=5)

bkg.SetSizer(vbox)
win.Show()
app.MainLoop()



你可能感兴趣的:(python wxpython--学习笔记)