管理系统界面设计python_python图形化界面设计(wxpython)一布局管理
本篇內容來自https://www.cnblogs.com/-wenli/p/12149850.html
1.布局分析和注意點
例程主要是Sizer布局應用。wx.BoxSizer的布局方向分為橫向和縱向兩種,并且可以在橫向或縱向方向上包含子sizer以創建嵌套的布局。
LeftPanel、CenterPanel、RightPanel三個面板采用橫向boxsizer布局。對應四個Box盒子,三個垂直盒子,一個水平盒子。
三個垂直盒子:
Boxv1布局LeftPanel
Boxv3布局CenterPanel
Boxv4布局RightPanel
一個水平盒子:
Boxh1布局三個垂直盒子
#創建三個面板
defInit_Panel(self):
self.LeftPanel=wx.Panel(self)
self.CenterPanel=wx.Panel(self)
self.RightPanel=wx.Panel(self)#創建四個盒子
#三個垂直盒子、一個水平盒子
defInit_Box(self):#三個垂直盒子
self.Boxv1 =wx.BoxSizer(wx.VERTICAL)
self.Boxv2=wx.BoxSizer(wx.VERTICAL)
self.Boxv3=wx.BoxSizer(wx.VERTICAL)#一個水平盒子
self.Boxh1 = wx.BoxSizer(wx.HORIZONTAL)
在水平盒子里布局三個垂直盒子
#將三個垂直盒子添加到垂直盒子
self.Boxh1.Add(self.LeftPanel,proportion = 1, border = 2,flag = wx.ALL |wx.EXPAND)
self.Boxh1.Add(self.CenterPanel, proportion=4, border=2, flag=wx.ALL |wx.EXPAND)
self.Boxh1.Add(self.RightPanel, proportion=1, border=2, flag=wx.ALL | wx.EXPAND)
兩個主要的參數:
border:邊框像素。
proportion:占空間的比率。
注意點:
2.頁面切換實現
程序:
#創建notebook
nb = wx.Notebook(self.CenterPanel, style=wx.NB_FIXEDWIDTH)
page1=PageOne(nb)
page2=PageTwo(nb)
page3=PageThree(nb)#在notebook中添加面板,第一個參數是面板,第二個參數是標簽
nb.AddPage(page1, "使用說明")
nb.AddPage(page2,"選擇檢索")
nb.AddPage(page3,"查看結果")
#本質:創建了三個面板###########################使用說明1######################################
classPageOne(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[0])
self.tx1= wx.StaticText(self, -1, "使用說明", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx1.SetFont(font)###########################使用說明2######################################
classPageTwo(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[1])
self.tx2= wx.StaticText(self, -1, "選擇檢索", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx2.SetFont(font)###########################使用說明3######################################
classPageThree(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[2])
self.tx3= wx.StaticText(self, -1, "查看結果", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx3.SetFont(font)
3.結合matplotlib
Matplotlib的常見對象Figure,FigureCanvas,Axes,給出最簡單的例程:
scores = [89, 98, 70, 80, 60, 78, 85, 90]
t_score= numpy.arange(1, len(scores) + 1, 1)
s_score=numpy.array(scores)
self.figure_score=Figure()
self.figure_score.set_figheight(3.6)
self.figure_score.set_figwidth(7.8)
self.axes_score= self.figure_score.add_subplot(111)#畫圖
self.axes_score.plot(t_score, s_score, 'ro', t_score, s_score, 'k')
self.axes_score.set_title(u'My Scores')
self.axes_score.grid(True)
self.axes_score.set_xlabel('T')
self.axes_score.set_ylabel('score')#在self.CenterPanel上顯示
cannvas = FigureCanvas(self.CenterPanel, -1, self.figure_score)
補充關于wx.Font
wx.Font 對象確定一個文本的外觀。可以在其他有文本顯示的控件比如 TextCtrl,Button,Label 通過 setFont(Font對象)來指明。
其構造函數如下:
wx.Font(pointSize,family,style,weight,underline,faceName,encoding)
參數 說明
pointSize pointSize是字體的以磅為單位的整數尺寸。
family family用于快速指定一個字體而無需知道該字體的實際的名字.字體的準確選擇依賴于系統和具體可用的字體.你所得到的精確的字體將依賴于你的系統。字體類別如下:wx.DECORATIVE:一個正式的,老的英文樣式字體。 wx.DEFAULT:系統默認字體。 wx.MODERN:一個單間隔(固定字符間距)字體。 wx.ROMAN:serif字體,通常類似于Times New Roman。 wx.SCRIPT:手寫體或草寫體。 wx.SWISS:sans-serif字體,通常類似于Helvetica或Arial。
style style參數指明字體的是否傾斜,它的值有:wx.NORMAL,wx.SLANT,wx.ITALIC
weight weight參數指明字體的醒目程度,他的值有:wx.NORMAL,wx.LIGHT,wx.BOLD
underline underline參數僅工作在Windows系統下,如果取值為True,則加下劃線,False為無下劃線。
faceName faceName參數指定字體名。
encoding encoding參數允許你在幾個編碼中選擇一個,它映射內部的字符和字本顯示字符。編碼不是Unicode編碼,只是用于wxPython的不同的8位編碼。大多數情況你可以使用默認編碼。
4.完整源碼
importwximportnumpyfrom matplotlib.backends.backend_wxagg importFigureCanvasWxAgg as FigureCanvasfrom matplotlib.figure importFigureclassExample(wx.Frame):def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.Init_Panel()
self.Init_Box()
self.Init_Left()
self.Init_Center()
self.Init_Right()#將三個垂直盒子添加到垂直盒子
self.Boxh1.Add(self.LeftPanel,proportion = 1, border = 2,flag = wx.ALL |wx.EXPAND)
self.Boxh1.Add(self.CenterPanel, proportion=4, border=2, flag=wx.ALL |wx.EXPAND)
self.Boxh1.Add(self.RightPanel, proportion=1, border=2, flag=wx.ALL |wx.EXPAND)#將垂直盒子和主框架關聯
self.SetSizer(self.Boxh1)#顯示主框架
self.Show()#創建三個面板
defInit_Panel(self):
self.LeftPanel=wx.Panel(self)
self.CenterPanel=wx.Panel(self)
self.RightPanel=wx.Panel(self)#創建四個盒子
#三個垂直盒子、一個水平盒子
defInit_Box(self):#三個垂直盒子
self.Boxv1 =wx.BoxSizer(wx.VERTICAL)
self.Boxv2=wx.BoxSizer(wx.VERTICAL)
self.Boxv3=wx.BoxSizer(wx.VERTICAL)#一個水平盒子
self.Boxh1 =wx.BoxSizer(wx.HORIZONTAL)defInit_Left(self):#創建一個wx.StaticBox對象。
#聲明一個wx.StaticBoxSizer與創建的wx.StaticBox對象作為其參數。
nm = wx.StaticBox(self.LeftPanel, -1, '參數輸入')
nmSizer=wx.StaticBoxSizer(nm, wx.VERTICAL)#構建靜態文本框
modelname = wx.StaticText(self.LeftPanel, -1, "模塊名稱")#在StaticBoxSizer添加文本框
nmSizer.Add(modelname, 1, wx.EXPAND | wx.ALL, 1)#創建下拉框
self.languages = ['NXP', 'MSP430', 'SMT32']
self.combo1= wx.ComboBox(self.LeftPanel, choices=self.languages, value=self.languages[0])#在StaticBoxSizer盒子添加下拉框
nmSizer.Add(self.combo1, 1, wx.EXPAND | wx.ALL, 1)#構建靜態文本框
modeltype = wx.StaticText(self.LeftPanel, -1, "模塊型號")#在StaticBoxSizer添加文本框
nmSizer.Add(modeltype, 1, wx.EXPAND | wx.ALL, 1)#創建下拉框
self.languages = ['0x123a', '0x3333', '0x1232']
self.combo2= wx.ComboBox(self.LeftPanel, choices=self.languages, value=self.languages[0])#在StaticBoxSizer添加下拉框
nmSizer.Add(self.combo2, 1, wx.EXPAND | wx.ALL, 1)#在垂直盒子添加StaticBoxSizer
self.Boxv1.Add(nmSizer,0, wx.EXPAND | wx.ALL,10)#創建RadioButton
lblList = ['線性網', '單環網', '雙環網','雙網冗余']
self.rbox= wx.RadioBox(self.LeftPanel, label='組網類型', choices=lblList, majorDimension=4,style=wx.RA_SPECIFY_ROWS)#在垂直盒子添加RadioButton
self.Boxv1.Add(self.rbox,0, wx.EXPAND | wx.ALL,10)#創建一個wx.StaticBox對象。
#聲明一個wx.StaticBoxSizer與創建的wx.StaticBox對象作為其參數。
nm1 = wx.StaticBox(self.LeftPanel, -1, '日志信息')
nmSizer1=wx.StaticBoxSizer(nm1, wx.VERTICAL)#創建文本域
self.multiText = wx.TextCtrl(self.LeftPanel, -1, style=wx.TE_MULTILINE) #創建一個文本控件
self.multiText.SetInsertionPoint(0) #設置插入點
nmSizer1.Add(self.multiText, 1, wx.EXPAND | wx.ALL, 10)#在垂直盒子里添加StaticBoxSizer盒子
self.Boxv1.Add(nmSizer1, 1, wx.EXPAND | wx.ALL, 10)#把垂直盒子與LeftPanel關聯起來
self.LeftPanel.SetSizer(self.Boxv1)defInit_Center(self):
self.CenterPanel.SetBackgroundColour('white')
scores= [89, 98, 70, 80, 60, 78, 85, 90]
t_score= numpy.arange(1, len(scores) + 1, 1)
s_score=numpy.array(scores)
self.figure_score=Figure()
self.figure_score.set_figheight(3.6)
self.figure_score.set_figwidth(7.8)
self.axes_score= self.figure_score.add_subplot(111)#畫圖
self.axes_score.plot(t_score, s_score, 'ro', t_score, s_score, 'k')
self.axes_score.set_title(u'My Scores')
self.axes_score.grid(True)
self.axes_score.set_xlabel('T')
self.axes_score.set_ylabel('score')#在self.CenterPanel上顯示
cannvas = FigureCanvas(self.CenterPanel, -1, self.figure_score)
self.Boxv2.Add(cannvas,1, wx.EXPAND | wx.ALL, 10)#創建notebook
nb = wx.Notebook(self.CenterPanel, style=wx.NB_FIXEDWIDTH)
page1=PageOne(nb)
page2=PageTwo(nb)
page3=PageThree(nb)#在notebook中添加面板,第一個參數是面板,第二個參數是標簽
nb.AddPage(page1, "使用說明")
nb.AddPage(page2,"選擇檢索")
nb.AddPage(page3,"查看結果")
self.Boxv2.Add(nb,1, wx.EXPAND | wx.ALL, 10)
self.CenterPanel.SetSizer(self.Boxv2)defInit_Right(self):
self._btn1= wx.Button(self.RightPanel, label=u'Diag', size=(50, 50))
self.Boxv3.Add(self._btn1, 0, wx.EXPAND| wx.ALL, 10)
self._btn2= wx.Button(self.RightPanel, label=u'Trend', size=(50, 50))
self.Boxv3.Add(self._btn2, 0, wx.EXPAND| wx.ALL, 10)
self._btn3= wx.Button(self.RightPanel, label=u'About', size=(50, 50))
self.Boxv3.Add(self._btn3, 0, wx.EXPAND| wx.ALL, 10)
self.RightPanel.SetSizer(self.Boxv3)#本質:創建了三個面板###########################使用說明1######################################
classPageOne(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[0])
self.tx1= wx.StaticText(self, -1, "使用說明", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx1.SetFont(font)###########################使用說明2######################################
classPageTwo(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[1])
self.tx2= wx.StaticText(self, -1, "選擇檢索", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx2.SetFont(font)###########################使用說明3######################################
classPageThree(wx.Panel):def __init__(self, parent):
wx.Panel.__init__(self, parent)
colour= [(160, 255, 204), (153, 204, 255), (151, 253, 225), ]
self.SetBackgroundColour(colour[2])
self.tx3= wx.StaticText(self, -1, "查看結果", (355, 45),
(100, -1), wx.ALIGN_CENTER)
font= wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.tx3.SetFont(font)
app=wx.App()
Example(None, title='布局實踐')
app.MainLoop()
View Code
總結
以上是生活随笔為你收集整理的管理系统界面设计python_python图形化界面设计(wxpython)一布局管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 嵌套 思维导图_Python 这方面,8
- 下一篇: 中文 代码 谷歌_Chrome谷歌浏览器