Python Qt GUI设计:窗口布局管理方法【强化】(基础篇—6)
目錄
1、?水平布局類(QHBoxLayout)
2、垂直布局類(QVBoxLayout)
3、網(wǎng)格布局類(QGridLayout)
3.1、單一的網(wǎng)絡(luò)布局
3.2、跨越行、列的網(wǎng)絡(luò)布局
4、表單布局類(QFormLayout)
5、嵌套布局
5.1、在布局中添加其他布局
5.2、在控件中添加布局
5.3、QSplitter布局管理器
在Python Qt GUI設(shè)計:窗口布局管理方法【基礎(chǔ)篇】(基礎(chǔ)篇—5)文章中,聊到了如何使用Qt Designer進(jìn)行窗口布局管理,其實在Qt Designer中可以非常方便進(jìn)行窗口布局管理設(shè)計,本篇博文在4種窗口布局方式基礎(chǔ)上繼續(xù)深入聊聊API函數(shù)~
在PyQt 5中有四種布局方式:水平布局、垂直布局、網(wǎng)格布局、表單布局,以及兩種布局方法,即 addLayout()和addWidget(),其中 addLayout()用于在布局中插入子布局,addWidget()用于在布局中插入控件。
四種布局方式對應(yīng)四個布局類:
- 水平布局類(QHBoxLayout),可以把所添加的控件在水平方向上依次排列;
- 垂直布局類(QVBoxLayout),可以把所添加的控件在垂直方向上依次排列;
- 網(wǎng)格布局類(QGridLayout),可以把所添加的控件以網(wǎng)格的形式排列;
- 表單布局類(QFormLayout),可以把所添加的控件以兩列的形式排列。
布局類及其子類的繼承關(guān)系如下圖所示:
1、?水平布局類(QHBoxLayout)
采用QHBoxLayout類,按照從左到右的順序來添加控件。QHBoxLayout類中的常用方法如下表所示:
在創(chuàng)建QHBoxLayout布局時用到的對齊方式參數(shù)如下表所示:
通過一個例子,了解水平布局使用,示例代碼如下所示:
import sys
from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton
from PyQt5.QtCore import Qt class Winform(QWidget):def __init__(self,parent=None):super(Winform,self).__init__(parent)self.setWindowTitle("水平布局管理例子") # 水平布局按照從左到右的順序進(jìn)行添加按鈕部件。hlayout = QHBoxLayout() hlayout.addWidget(QPushButton(str(1)))hlayout.addWidget(QPushButton(str(2)))hlayout.addWidget(QPushButton(str(3)))hlayout.addWidget(QPushButton(str(4))) hlayout.addWidget(QPushButton(str(5))) #設(shè)置控件間的間距hlayout.setSpacing( 0 ) self.setLayout(hlayout) if __name__ == "__main__": app = QApplication(sys.argv) form = Winform()form.show()sys.exit(app.exec_())
運行效果如下所示:?
2、垂直布局類(QVBoxLayout)
采用QVBoxLayout類,按照從上到下的順序添加控件。QHBoxLayout和QVBoxLayout類都繼承自QBoxLayout類,所以常用方法也是相同的。
通過一個例子,了解垂直布局使用,示例代碼如下所示:
import sys
from PyQt5.QtWidgets import QApplication ,QWidget ,QVBoxLayout , QPushButtonclass Winform(QWidget):def __init__(self,parent=None):super(Winform,self).__init__(parent)self.setWindowTitle("垂直布局管理例子") self.resize(330, 150) # 垂直布局按照從上到下的順序進(jìn)行添加按鈕部件。vlayout = QVBoxLayout()vlayout.addWidget( QPushButton(str(1)))vlayout.addWidget( QPushButton(str(2)))vlayout.addWidget( QPushButton(str(3)))vlayout.addWidget( QPushButton(str(4)))vlayout.addWidget( QPushButton(str(5)))self.setLayout(vlayout) if __name__ == "__main__": app = QApplication(sys.argv) form = Winform()form.show()sys.exit(app.exec_())
運行效果如下所示:??
3、網(wǎng)格布局類(QGridLayout)
QGridLayout(網(wǎng)格布局)是將窗口分隔成行和列的網(wǎng)格來進(jìn)行排列。通常可以使用函數(shù)addWidget()將被管理的控件(Widget)添加到窗口中,或者使用addLayout()函數(shù)將布局(Layout)添加到窗口中。也可以通過addWidget()函數(shù)對所添加的控件設(shè)置行數(shù)和列數(shù)的跨越,最后實現(xiàn)網(wǎng)格占據(jù)多個窗格。
QGridLayout類中的常用方法如下表所示:
3.1、單一的網(wǎng)絡(luò)布局
來做個單一網(wǎng)格布局的小案例,創(chuàng)建QGridLayout的實例,并設(shè)置為窗口的布局,創(chuàng)建按鈕的標(biāo)簽列表,在網(wǎng)格中創(chuàng)建一個位置列表,創(chuàng)建按鈕,并通過addWidget()方法添加到布局中,示例如下所示:
import sys
from PyQt5.QtWidgets import QApplication ,QWidget , QGridLayout, QPushButtonclass Winform(QWidget):def __init__(self,parent=None):super(Winform,self).__init__(parent)self.initUI()def initUI(self): #創(chuàng)建QGridLayout的實例,并設(shè)置為窗口的布局grid = QGridLayout() self.setLayout(grid) #創(chuàng)建按鈕的標(biāo)簽列表names = ['Cls', 'Back', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+'] #在網(wǎng)格中創(chuàng)建一個位置列表positions = [(i,j) for i in range(5) for j in range(4)] #創(chuàng)建按鈕,并通過addWidget()方法添加到布局中for position, name in zip(positions, names): if name == '': continue button = QPushButton(name) grid.addWidget(button, *position) self.move(300, 150) self.setWindowTitle('網(wǎng)格布局管理例子') if __name__ == "__main__": app = QApplication(sys.argv) form = Winform()form.show()sys.exit(app.exec_())
運行效果如下所示:???
3.2、跨越行、列的網(wǎng)絡(luò)布局
本示例將實現(xiàn)跨越行、列的網(wǎng)絡(luò)單元格設(shè)計,示例如下所示:
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplication) class Winform(QWidget):def __init__(self,parent=None):super(Winform,self).__init__(parent)self.initUI()def initUI(self): titleLabel = QLabel('標(biāo)題') authorLabel = QLabel('提交人') contentLabel = QLabel('申告內(nèi)容') titleEdit = QLineEdit() authorEdit = QLineEdit() contentEdit = QTextEdit() grid = QGridLayout() grid.setSpacing(10) #把titleLabel放在QGridLayout布局的第1行第0列。grid.addWidget(titleLabel, 1, 0) #把titleEdit放在QGridLayout布局的第1行第1列。grid.addWidget(titleEdit, 1, 1) #把authorLabel放在QGridLayout布局的第2行第0列。grid.addWidget(authorLabel, 2, 0) #把authorEdit放在QGridLayout布局的第2行第1列。grid.addWidget(authorEdit, 2, 1) #把contentLabel放在QGridLayout布局的第3行第0列。grid.addWidget(contentLabel, 3, 0) #把contentEdit放在QGridLayout布局的第3行第1列,跨越5行1列。grid.addWidget(contentEdit, 3, 1, 5, 1) self.setLayout(grid) self.setGeometry(300, 300, 350, 300) self.setWindowTitle('故障申告')if __name__ == "__main__": app = QApplication(sys.argv) form = Winform()form.show()sys.exit(app.exec_())
運行效果如下所示:????
4、表單布局類(QFormLayout)
QFormLayout是label-field式的表單布局,顧名思義,就是實現(xiàn)表單方式的布局。
表單是提示用戶進(jìn)行交互的一種模式,其主要由兩列組成:第一列用于顯示信息,給用戶提示,一般叫作label域;第二列需要用戶進(jìn)行選擇或輸入,一般叫作field域。label與field的關(guān)系就是label關(guān)聯(lián)field。示例如下所示:
import sys
from PyQt5.QtWidgets import QApplication ,QWidget ,QFormLayout , QLineEdit, QLabelclass Winform(QWidget):def __init__(self,parent=None):super(Winform,self).__init__(parent)self.setWindowTitle("表單布局管理例子") self.resize(400, 100) fromlayout = QFormLayout()labl1 = QLabel("標(biāo)簽1")lineEdit1 = QLineEdit()labl2 = QLabel("標(biāo)簽2")lineEdit2 = QLineEdit()labl3 = QLabel("標(biāo)簽3")lineEdit3 = QLineEdit()fromlayout.addRow(labl1, lineEdit1)fromlayout.addRow(labl2, lineEdit2)fromlayout.addRow(labl3, lineEdit3)self.setLayout(fromlayout) if __name__ == "__main__": app = QApplication(sys.argv) form = Winform()form.show()sys.exit(app.exec_())
?運行效果如下所示:????
5、嵌套布局
在窗口中進(jìn)行單一的布局并不難,但若是進(jìn)行比較復(fù)雜的布局,就涉及布局的嵌套了,推薦使用Qt Designer的可視化管理工具來進(jìn)行界面布局,可參見上篇博文。
本文僅介紹API函數(shù)實現(xiàn)嵌套布局的示例方法。
5.1、在布局中添加其他布局
整個例子,首先全局布局采用的是水平布局,局部布局采用的分別是水平布局、垂直布局、網(wǎng)格布局和表單布局,準(zhǔn)備4個QWidget控件:hwg、vwg、gwg和formlayout,使用4個QWidget控件分別設(shè)置局部布局,接下來,將4個QWidget控件添加到全局變量中,最后,把全局布局應(yīng)用到窗口本身。
示例效果如下所示:
實現(xiàn)代碼如下所示:
import sys
from PyQt5.QtWidgets import QApplication ,QWidget , QHBoxLayout, QVBoxLayout, QGridLayout , QFormLayout, QPushButton class MyWindow( QWidget): def __init__(self): super().__init__()self.setWindowTitle('嵌套布局示例')# 全局布局(1個):水平wlayout = QHBoxLayout() # 局部布局(4個):水平、豎直、網(wǎng)格、表單hlayout = QHBoxLayout()vlayout = QVBoxLayout()glayout = QGridLayout()formlayout = QFormLayout()# 局部布局添加部件(例如:按鈕)hlayout.addWidget( QPushButton(str(1)) ) hlayout.addWidget( QPushButton(str(2)) )vlayout.addWidget( QPushButton(str(3)) )vlayout.addWidget( QPushButton(str(4)) )glayout.addWidget( QPushButton(str(5)) , 0, 0 )glayout.addWidget( QPushButton(str(6)) , 0, 1 )glayout.addWidget( QPushButton(str(7)) , 1, 0)glayout.addWidget( QPushButton(str(8)) , 1, 1)formlayout.addWidget( QPushButton(str(9)) )formlayout.addWidget( QPushButton(str(10)) )formlayout.addWidget( QPushButton(str(11)) )formlayout.addWidget( QPushButton(str(12)) )# 準(zhǔn)備四個部件hwg = QWidget() vwg = QWidget()gwg = QWidget()fwg = QWidget()# 四個部件設(shè)置局部布局hwg.setLayout(hlayout) vwg.setLayout(vlayout)gwg.setLayout(glayout)fwg.setLayout(formlayout)# 四個部件加至全局布局wlayout.addWidget(hwg)wlayout.addWidget(vwg)wlayout.addWidget(gwg)wlayout.addWidget(fwg)# 窗體本體設(shè)置全局布局self.setLayout(wlayout) if __name__=="__main__": app = QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_())
這樣的布局有一個缺點:4種局部布局需要4個空白控件,假如有10種局部布局,就需要10個空白控件。怎么解決這個問題呢??這時候就需要在控件中添加布局。
5.2、在控件中添加布局
在控件中添加布局,可以不管有多少種局部布局,只需要一個空白控件,然后在這個空白控件中進(jìn)行多種布局就可以實現(xiàn)嵌套布局的效果。
對5.1中的示例進(jìn)行優(yōu)化,先準(zhǔn)備一個全局控件,用于添加全局布局,定義全局布局和4種局部布局,在局部布局中放置一些按鈕控件,最后把4種局部布局添加到全局布局中。實現(xiàn)代碼如下所示:
from PyQt5.QtWidgets import *
import sys class MyWindow(QWidget): def __init__(self): super().__init__()self.setWindowTitle('嵌套布局示例')self.resize(700, 200)# 全局部件(注意參數(shù) self),用于"承載"全局布局wwg = QWidget(self)# 全局布局(注意參數(shù) wwg)wl = QHBoxLayout(wwg)hlayout = QHBoxLayout()vlayout = QVBoxLayout()glayout = QGridLayout()formlayout = QFormLayout()# 局部布局添加部件(例如:按鈕)hlayout.addWidget( QPushButton(str(1)) )hlayout.addWidget( QPushButton(str(2)) )vlayout.addWidget( QPushButton(str(3)) )vlayout.addWidget( QPushButton(str(4)) )glayout.addWidget( QPushButton(str(5)) , 0, 0 )glayout.addWidget( QPushButton(str(6)) , 0, 1 )glayout.addWidget( QPushButton(str(7)) , 1, 0)glayout.addWidget( QPushButton(str(8)) , 1, 1)formlayout.addWidget( QPushButton(str(9)) )formlayout.addWidget( QPushButton(str(10)) )formlayout.addWidget( QPushButton(str(11)) )formlayout.addWidget( QPushButton(str(12)) )# 這里向局部布局內(nèi)添加部件,將他加到全局布局wl.addLayout(hlayout) wl.addLayout(vlayout)wl.addLayout(glayout)wl.addLayout(formlayout) if __name__=="__main__": app = QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_())
5.3、QSplitter布局管理器
除了上面介紹的Layout布局管理,PyQt還提供了一個特殊的布局管理器:QSplitter,它可以動態(tài)地拖動子控件之間的邊界,算是一個動態(tài)的布局管理器。
QSplitter 允許用戶通過拖動子控件的邊界來控制子控件的大小,并提供了一個處理拖曳子控件的控制器。
在QSplitter對象中各子控件默認(rèn)是橫向布局的,可以使用Qt.Vertical進(jìn)行垂直布局。QSplitter類中的常用方法如下表所示:
通過一個例子,了解QSplitter布局的使用,在這個例子中,顯示了使用兩個QSplitter組織的兩個QFrame控件。其中第一個QSplitter對象包含了一個QFrame對象和QTextEdit對象,并按照水平方向進(jìn)行布局。第二個QSplitter對象添加了第一個QSplitter對象和另一個QFrame對象,并按照垂直方向進(jìn)行布局。
示例效果如下所示:
示例代碼如下所示:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *class SplitterExample(QWidget):def __init__(self):super(SplitterExample, self).__init__()self.initUI()def initUI(self): hbox = QHBoxLayout(self)self.setWindowTitle('QSplitter 布局例子')self.setGeometry(300, 300, 300, 200) topleft = QFrame()topleft.setFrameShape(QFrame.StyledPanel)bottom = QFrame()bottom.setFrameShape(QFrame.StyledPanel)splitter1 = QSplitter(Qt.Horizontal)textedit = QTextEdit()splitter1.addWidget(topleft)splitter1.addWidget(textedit)splitter1.setSizes([100,200])splitter2 = QSplitter(Qt.Vertical)splitter2.addWidget(splitter1)splitter2.addWidget(bottom)hbox.addWidget(splitter2)self.setLayout(hbox)if __name__ == '__main__':app = QApplication(sys.argv)demo = SplitterExample()demo.show()sys.exit(app.exec_())
總結(jié)
以上是生活随笔為你收集整理的Python Qt GUI设计:窗口布局管理方法【强化】(基础篇—6)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何设计电桥传感器驱动电路?
- 下一篇: Python Qt GUI设计:如何调整