国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例

瀏覽:29日期:2022-08-03 15:02:42
PyQt5布局控件QGridLayout簡介

QGridLayout(網(wǎng)格布局)是將窗口分割成行和列的網(wǎng)格來進(jìn)行排列,通常可以使用函數(shù)addWidget()將被管理的控件(Widget)添加到窗口中,或者使用addLayout()函數(shù)將布局(layout)添加到窗口中,也可以通過addWIdget()函數(shù)對(duì)所添加的控件設(shè)置行數(shù)與列數(shù)的跨越,最后實(shí)現(xiàn)網(wǎng)格占據(jù)多個(gè)窗格

QGridLayout類中常用的方法 方法 描述 addWidget(QWidget Widget,int row,int col,int alignment=0) 給網(wǎng)格布局添加部件,設(shè)置指定的行和列,起始位置的默認(rèn)值為(0,0) widget:所添加的控件 row:控件的行數(shù),默認(rèn)從0開始 column:控件的列數(shù),默認(rèn)從0開始 alignment:對(duì)齊方式 addWidget(QWidget widget,int fromRow,int fromColulmn,int rowSpan,int columnSpan,Qt.Alignment alignment=0) 所添加的的控件跨越很多行或者列的時(shí)候,使用這個(gè)函數(shù) widget:所添加的控件 fromRow:控件的起始行數(shù) fronColumn:控件的起始列數(shù) rowSpan:控件跨越的行數(shù) column:控件跨越的列數(shù) alignment:對(duì)齊方式 setSpacing(int spacing) 設(shè)置軟件在水平和垂直方向的間隔 QGridLayout單一的網(wǎng)格單元格實(shí)例

import sysfrom PyQt5.QtWidgets import QApplication ,QWidget , QGridLayout, QPushButtonclass Winform(QWidget): def __init__(self,parent=None): super(Winform,self).__init__(parent) self.initUI() def initUI(self): #1創(chuàng)建QGridLayout的實(shí)例,并設(shè)置窗口的布局 grid = QGridLayout() self.setLayout(grid) #2創(chuàng)建按鈕的標(biāo)簽列表 names = [’Cls’, ’Back’, ’’, ’Close’, ’7’, ’8’, ’9’, ’/’, ’4’, ’5’, ’6’, ’*’, ’1’, ’2’, ’3’, ’-’, ’0’, ’.’, ’=’, ’+’] #3 在網(wǎng)格中創(chuàng)建一個(gè)位置列表positions = [(i,j) for i in range(5) for j in range(4)] #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_())

運(yùn)行效果圖如下

python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例

第一組代碼:創(chuàng)建QGridLayout的實(shí)例,并設(shè)置窗口的布局

第二組代碼:創(chuàng)建按鈕的標(biāo)簽列表

第三組代碼:在網(wǎng)格中創(chuàng)建一個(gè)位置列表

第四組代碼:創(chuàng)建按鈕并通過addWIdget()方法添加到布局中

QGridLayout跨越行和列的網(wǎng)格單元格實(shí)例

import sysfrom 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) grid.addWidget(titleLabel, 1, 0) grid.addWidget(titleEdit, 1, 1) grid.addWidget(authorLabel, 2, 0) grid.addWidget(authorEdit, 2, 1) grid.addWidget(contentLabel, 3, 0) 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_())

運(yùn)行效果示意圖如下

python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例

代碼分析

把titleLabel放在QGridLayout布局的第一行第0列

grid.addWidget(titleLabel, 1, 0)  

把titleEditl放在QGridLayout布局的第一行第1列

grid.addWidget(titleEdit, 1, 1)  

把contentLabel放在QGridLayout布局的第3行第0列

grid.addWidget(contentLabel, 3, 0)  

把contentEdit放在QGridLayout布局的第3行第1列,跨越5行1列

grid.addWidget(contentEdit, 3, 1, 5, 1)

本文主要介紹了PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5布局控件知識(shí)請(qǐng)查看下面的相關(guān)鏈接

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 独山县| 镇巴县| 亳州市| 治多县| 凤翔县| 平阳县| 三原县| 诸暨市| 日照市| 八宿县| 仁怀市| 佛坪县| 秦安县| 尼玛县| 伊金霍洛旗| 商丘市| 绥中县| 法库县| 万州区| 鹿邑县| 获嘉县| 同德县| 新化县| 定襄县| 塔河县| 双峰县| 临泽县| 花莲市| 衡阳市| 阿合奇县| 韶山市| 昭通市| 西贡区| 铁岭县| 积石山| 个旧市| 诸暨市| 富裕县| 漾濞| 安阳县| 城市|