WxPythonBoxSizer布局详解
boxsizer分为横向和纵向两种,如果你想要把控件⽔平排列,就使⽤横向boxsizer,如果你想让控件都垂直排列,就使⽤纵向boxsizer,当然很多界⾯既有横向排列⼜有纵向排列的控件,我们就可以使⽤boxsizer的嵌套。
思路分析:
整体思路:
先整个窗⼝Frame
在窗⼝上建⽴⼀个⾯板Panel,⽤于放控件(Button,StaticText,TextCtl,CheckBox等控件),尺⼨器(横向和纵向BoxSizer,相互嵌套使⽤)
最后把⼀些标签,控件放置在横向或纵向BoxSidzer中,进⾏布局;
具体说明:
创建⼀个sizer的步骤:
1.创建你想⽤来⾃动调⽤尺⼨的panel或container(容器)。
2.创建sizer。
3.创建你的⼦窗⼝。
4.使⽤sizer的Add()⽅法来将每个⼦窗⼝添加给sizer。
sizer可以嵌套,这意味你可以像窗⼝对象⼀样添加别的sizer到⽗sizer。
5.调⽤容器的SetSizer(sizer)⽅法。
最常⽤的wxPython的sizer:
1.wx.BoxSizer:在⼀条线上布局⼦窗⼝部件。wx.BoxSizer的布局⽅向可以是⽔平或坚直的,并且可以在⽔平或坚直⽅向上包含⼦sizer以创建复杂的布局。在项⽬被添加时传递给sizer的参数控制⼦窗⼝部件如何根据box的主体或垂直轴线作相应的尺⼨调整。
2. wx.FlexGridSizer:⼀个固定的⼆维⽹格,它与wx.GridSizer的区别是,⾏和列根据所在⾏或列的最⼤元素分别被设置。
3. wx.GridSizer:⼀个固定的⼆维⽹格,其中的每个元素都有相同的尺⼨。当创建⼀个grid sizer时,你要么固定⾏的数量,要么固定列的数量。项⽬被从左到右的添加,直到⼀⾏被填满,然后从下⼀⾏开始。
flex布局详细讲解
4. wx.GridBagSizer:⼀个固定的⼆维⽹格,基于wx.FlexGridSizer。允许项⽬被放置在⽹格上的特定点,也允许项⽬跨越多和⽹格区域。
5. wx.StaticBoxSizer:等同于wx.BoxSizer,只是在box周围多了⼀个附加的边框(有⼀个可选的标签)。
wx.Sizer的⽅法:
Add(size, proportion=0,flag=0, border=0,userData=None):
第⼀个添加⼀个wxWindow,
第⼆个添加⼀个嵌套的sizer,第三个添加空的空间,⽤作分隔符。参数proportion管理窗⼝总尺⼨,它是相对于别的窗⼝的改变⽽⾔的,它只对wx.BoxSizer有意义。参数flag是⼀个位图,针对对齐、边框位置,增长有许多不同的标志。参数border是窗⼝或sizer周围以像素为单位的空间总量。userData使你能够将对象与数据关联,例如,在⼀个⼦类中,可能需要更多的⽤于尺⼨的信息。
Fit(window)
FitInside(window ):调整window尺⼨以匹配sizer认为所需要的最⼩化尺⼨。
这个参数的值通常是使⽤sizer的窗⼝。
FitInside()是⼀个类似的⽅法,只不过将改变窗⼝在屏幕上的显⽰替换为只改变它的内部实现。它⽤于scroll panel中的窗⼝以触发滚动栏的显⽰。
GetSize():以wx.Size对象的形式返回sizer的尺⼨。
GetPosition():以wx.Point对象的形式返回sizer的位置。
GetMinSize():以wx.Size对象的形式返回完全填充sizer所需的最⼩尺⼨。
Layout():强迫sizer去重新计算它的孩⼦的尺⼨和位置。在动态地添加或删除了⼀个孩⼦之后调⽤。
Prepend(…):与Add()相同(只是为了布局的⽬的,把新的对象放在sizer列表的开头)。
Remove(window)
Remove(sizer)
Remove(nth):从sizer中删除⼀个对象。
SetDimension(x, y, width,height):强迫sizer按照给定的参数重新定位它的所有孩⼦。
详细参照:
代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,time
import wx,telnetlib
import win32api,win32con
import re,os
import re,os
#远程telnet进⼊OLT命令⾏
def TelnetOpen(event):
print("")
def DrawPanel():
#设置画布上⾯的控制信息
app=wx.App()
win= wx.Frame(None,title='OLT Auto Config ONU-Shelly',size=(850,700))
#新增⼀张画布
panel = wx.Panel(win)
# panel.SetBackgroundColour('yellow')
#选择中兴C300还是华为56XX OLT系列
oltText = wx.StaticText(panel,label=u'OLT VendorID:')
zte_checkBox = wx.CheckBox(panel,label=u"ZTE OLT")
hw_checkBox = wx.CheckBox(panel,label=u"HUAWEI OLT")
#设置IP地址进⾏telnet登录
staticText = wx.StaticText(panel,label=u'IP_Address:')
ip_textCtl = wx.TextCtrl(panel)
#在Panel创建⼀些组件如按钮,⽂本框
login_textCtl = wx.TextCtrl(panel)
staticText10 = wx.StaticText(panel,label=u'Account:')
pwd_textCtl = wx.TextCtrl(panel)
staticText12 = wx.StaticText(panel,label=u'Password:')
telnetBtn = wx.Button(panel,label=u'Telnet_OLT')
#打开按纽绑定事件函数,处理操作
telnetBtn.Bind(wx.EVT_BUTTON,TelnetOpen)
#给控件设置背景颜⾊:如telnetBtn设置绿⾊背景,如图
telnetBtn.SetBackgroundColour("Green")
shelf_textCtl = wx.TextCtrl(panel)
staticText1 = wx.StaticText(panel,label=u'Shelf_No:')
slot_textCtl = wx.TextCtrl(panel)
staticText2 = wx.StaticText(panel,label=u'Slot_No:')
pon_textCtl = wx.TextCtrl(panel)
staticText3 = wx.StaticText(panel,label=u'Port_No:')
onu_textCtl = wx.TextCtrl(panel)
staticText4 = wx.StaticText(panel,label=u'ONU_ID:')
vlan_textCtl = wx.TextCtrl(panel)
staticText11 = wx.StaticText(panel,label=u'VlanID:')
onutypeText = wx.StaticText(panel,label=u'PON Type:')
gpon_checkBox = wx.CheckBox(panel,label=u"GPON")
epon_checkBox = wx.CheckBox(panel,label=u"EPON")
showButton = wx.Button(panel,label=u'ShowOLTCfg')
vlanButton = wx.Button(panel,label=u'VlanCreate')
showvidButton = wx.Button(panel,label=u'ShowVidCfg')
untagButton = wx.Button(panel,label=u'UplinkUntagCfg')
tagButton = wx.Button(panel,label=u'UplinkTagCfg')
#添加⼀个标签
staticText5 = wx.StaticText(panel,label=u'Note:Click the ShowOLTCfg button to view the configuration in each case based on the configured shelf, slot, pon  port, ONUID, including the configuration of the uplink interface.\nlike:show run interface gpon_olt_1/15/1 and show run interface gpon_olt_1/15/1:1 and sh ow run inter gei_1/19/1 ',pos=(1,107),size=(650,50))
onuregisterText = wx.StaticText(panel,label=u'Note:The ONU registration process is as follows:')
#SN 复制
copySnText = wx.StaticText(panel,label=u'2.Copy the already found SN(MAC) to the textBox:')
sn_textCtl = wx.TextCtrl(panel)
changeOnuText = wx.StaticText(panel,label=u'Exec Replace_ONU:')
changeButton = wx.Button(panel,label=u'Replace ONU')
#添加两个按钮,⼀个⽤于显⽰配置,⼀个⽤于⼀键⾃动化配置ONU信息
findButton = wx.Button(panel,label=u'1.Find unauth ONU')
findButton = wx.Button(panel,label=u'1.Find unauth ONU')
registerButton = wx.Button(panel,label=u'3.Register ONU')
cfgButton = wx.Button(panel,label=u'4.Config ONU')
viewButton = wx.Button(panel,label=u'Find all_Online_ONU')
deleteButton = wx.Button(panel,label=u'Delete_ONU')
#修改控件的⽗对象改成panel ⽽不是Frame,Panel的⽗对象是Frame
dbaTest1 = wx.StaticText(panel,label='ZTE Config Parameter:')
dbaTest = wx.StaticText(panel,label='1.DBA:')
dba100M_checkBox = wx.CheckBox(panel,label=u"100M")
dba1000M_checkBox = wx.CheckBox(panel,label=u"1G")
onutypeTest = wx.StaticText(panel,label='2.ONU_Type:')
onutype_textCtl = wx.TextCtrl(panel)
onutypeButton = wx.Button(panel,label=u'Create ONU_TYPE')
#HUAWEI 配置参数
dbaTest2 = wx.StaticText(panel,label='HUAWEI Config Parameter:')
dbaTest3 = wx.StaticText(panel,label='1.DBA:')
dba100M_checkBox1 = wx.CheckBox(panel,label=u"100M")
dba1000M_checkBox1 = wx.CheckBox(panel,label=u"1G")
#HUAWEI模版
Test2 = wx.StaticText(panel,label='2.ont-lineprofile-id:')
onuline_textCtl = wx.TextCtrl(panel)
onulineButton = wx.Button(panel,label=u'Create linePro')
Test3 = wx.StaticText(panel,label='3.ont-srvprofile-id:')
onusrv_textCtl = wx.TextCtrl(panel)
onusrvButton = wx.Button(panel,label=u'Create SrvPro')
#⼀个⼤的显⽰⽂本框
showContent = wx.TextCtrl(panel,style=wx.TE_MULTILINE | wx.HSCROLL,size=(850,300)) #增加可移动下拉
#开始⾯板上布局
#实例化⼀个尺⼨器,默认⽔平
#proportion:相对⽐例:只能设置 0,1,2,3等,0是保持本⾝⼤⼩,按⽐例显⽰⼤⼩,1⽐0的宽⼀倍,以此类推#flag:填充的样式和⽅向,wx.EXPAND为完整填充,wx.ALL为填充的⽅向
#border:边框
box = wx.BoxSizer() #默认是创建横向BoxSizer
box.Add(oltText,proportion = 1,flag = wx.EXPAND|wx.ALIGN_RIGHT,border = 3)
#box.AddStretchSpacer(1) #将第⼀个控件加如伸缩控件
box.Add(zte_checkBox,proportion = 1,flag = wx.EXPAND|wx.ALIGN_LEFT,border = 3)
box.Add(hw_checkBox,proportion = 1,flag = wx.EXPAND|wx.ALIGN_LEFT,border = 3)
box1 = wx.BoxSizer()
box1.Add(staticText,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(ip_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(staticText10,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(login_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(staticText12,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(pwd_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box1.Add(telnetBtn,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 0)
box2 = wx.BoxSizer()
box2.Add(staticText1,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(shelf_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(staticText2,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(slot_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(staticText3,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(pon_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(staticText4,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(onu_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(staticText11,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box2.Add(vlan_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box3 = wx.BoxSizer()
box3.Add(onutypeText,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(gpon_checkBox,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(gpon_checkBox,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(epon_checkBox,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(showButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(vlanButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(showvidButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(untagButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box3.Add(tagButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box4 = wx.BoxSizer()
box4.Add(copySnText,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box4.Add(sn_textCtl,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box4.Add(changeOnuText,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box4.Add(changeButton,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box5 = wx.BoxSizer()
box5.Add(findButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box5.Add(registerButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box5.Add(cfgButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box5.Add(viewButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box5.Add(deleteButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box6 = wx.BoxSizer()
box6.Add(dbaTest1,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(dbaTest,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(dba100M_checkBox,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(dba1000M_checkBox,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(onutypeTest,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(onutype_textCtl,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box6.Add(onutypeButton,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box7 = wx.BoxSizer()
box7.Add(dbaTest2,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box7.Add(dbaTest3,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box7.Add(dba100M_checkBox1,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box7.Add(dba1000M_checkBox1,proportion = 0,flag = wx.EXPAND|wx.ALL,border = 3)
box8 = wx.BoxSizer()
box8.Add(Test2,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box8.Add(onuline_textCtl,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box8.Add(onulineButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box8.Add(Test3,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box8.Add(onusrv_textCtl,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
box8.Add(onusrvButton,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)
# box9 = wx.BoxSizer()
# box9.Add(showContent,proportion = 3,flag = wx.EXPAND|wx.ALL,border = 3)
#创建⼀个垂直尺⼨器,把⼀些横向Box或控件都装载其中
v_box = wx.BoxSizer(wx.VERTICAL)
v_box.Add(box,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)# 随着窗⼝托⼤控件⼀直保持居中或居左v_box.Add(box1,proportion = 0,flag = wx.ALIGN_LEFT ,border = 3)
v_box.Add(box2,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box3,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(staticText5,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(onuregisterText,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box4,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box5,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box6,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box7,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(box8,proportion = 0,flag = wx.ALIGN_LEFT,border = 3)
v_box.Add(showContent,proportion = 5,flag = wx.ALIGN_LEFT,border = 3)
'''
flag 参数详解:
wx.EXPAND|wx.ALL 会随着窗⼝变⼤⽽变化
wx.ALIGN_CENTER  保持居中
wx.ALIGN_LEFT  居左
wx.ALIGN_LEFT  居左
wx.ALIGN_RIGHT 居右
wx.ALIGN_TOP  置顶
wx.ALIGN_BOTTOM  置底部
wx.ALIGN_CENTER_VERTICAL  垂直居中wx.ALIGN_CENTER_HORIZONTAL  ⽔平居中wx.ALIGN_CENTER  居中
'''
#设置画布的主尺⼨器(⼀层包⼀层)panel.SetSizer(v_box)
win.Show()
app.MainLoop()
if name == ‘main’:
DrawPanel()
结果演⽰:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。