27-python_GUI-布局管理-事件处理

1. wxPython-布局管理


    尺寸器 

     - sizer, wx.BoxSizer    
     - >>> help(wx.BoxSizer)

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

     - 用法:
        hbox = wx.BoxSizer([wx.HORIZONTAL]) 
        vbox = wx.BoxSizer(wx.VERTICAL)

     - 增加panel以利于布局
     
     - 添加组件进行管理
        sizer.add(组件,proportion=,flag=,border=) 

    例子
#-*- encoding:UTF-8 -*-
import wx

app = wx.App()
win = wx.Frame(None, title="My Notebook", size=(410, 335))

panel = wx.Panel(win)

openButton = wx.Button(panel, label="Open")
saveButton = wx.Button(panel, label="Save")
filepathTxt = wx.TextCtrl(panel)
contentTxt = wx.TextCtrl(panel, style=wx.TE_MULTILINE)

#默认水平, 放置 文件路径文本框 打开按钮 保存按钮
# proportion : 占总体的 几分之1
# flag : expand 扩展
hbox = wx.BoxSizer() 
hbox.Add(filepathTxt, proportion=1, flag=wx.EXPAND)
hbox.Add(openButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)

#垂直, 
bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND|wx.ALL, border=5)
bbox.Add(contentTxt, proportion=1,flag=wx.EXPAND|wx.LEFT|wx.BOTTOM|wx.RIGHT,border=5)


panel.SetSizer(bbox)

win.Show()
app.MainLoop()



2. wxPython-事件处理

    

    通过组件的 Bind(func) 绑定事件处理函数
    如, 点击按钮时往控制台打印"hello"

    def printHelloEvtHandle(evt) :
        print "hello"
    btn.Bind(wx.EVT_BUTTON, printHelloEvtHandle)        

    举例:

#-*- encoding:UTF-8 -*-
import wx

def openFileEventHandle(evt) :
    filepath = filepathTxt.GetValue()
    fopen = open(filepath)
    content = fopen.read()
    fopen.close()
    contentTxt.SetValue(content)

def saveFileEventHandle(evt) :
    filepath = filepathTxt.GetValue()
    fopen = open(filepath, "w")
    content = contentTxt.GetValue()
    fopen.write(content)
    fopen.close()

app = wx.App()
win = wx.Frame(None, title="My Notebook", size=(410, 335))

panel = wx.Panel(win)

openButton = wx.Button(panel, label="Open")
saveButton = wx.Button(panel, label="Save")
filepathTxt = wx.TextCtrl(panel)
contentTxt = wx.TextCtrl(panel, style=wx.TE_MULTILINE)


#绑定事件
openButton.Bind(wx.EVT_BUTTON, openFileEventHandle)
saveButton.Bind(wx.EVT_BUTTON, saveFileEventHandle)


#默认水平, 放置 文件路径文本框 打开按钮 保存按钮
# proportion : 占总体的 几分之1
# flag : expand 扩展
hbox = wx.BoxSizer() 
hbox.Add(filepathTxt, proportion=1, flag=wx.EXPAND)
hbox.Add(openButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)

#垂直, 
bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND|wx.ALL, border=5)
bbox.Add(contentTxt, proportion=1,flag=wx.EXPAND|wx.LEFT|wx.BOTTOM|wx.RIGHT,border=5)


panel.SetSizer(bbox)

win.Show()
app.MainLoop()



你可能感兴趣的:(python,GUI,事件处理,布局管理)