PyQt6 布局管理器的六種實(shí)現(xiàn)示例
PyQt布局管理器有六種:
- move()方法布局
- 垂直布局管理器QVBoxLayout()
- 水平布局管理器QHBoxLayout()
- 表單布局管理器QFormLayout()
- 網(wǎng)格布局管理器QGridLayout()
- 布局嵌套
move()方法
根據(jù)窗口坐標(biāo)布局,類似Tk的place。
import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.resize(200,200)
label1 = QLabel('username:',self)
entry1 = QLineEdit(self)
# label2 = QLabel('password', self)
# entry2 = QLineEdit(self)
# 布局
label1.move(40,40)
entry1.move(80,40)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())垂直布局管理器QVBoxLayout()
垂直:Vertical Horizontal: 水平
Y 與 V 有點(diǎn)類似,V就是Y 垂直布局.

import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel("username:", self)
entry1 = QLineEdit(self)
label2 = QLabel("password:", self)
entry2 = QLineEdit(self)
# 布局
v_layout = QVBoxLayout()
v_layout.addWidget(label1)
v_layout.addWidget(entry1)
v_layout.addWidget(label2)
v_layout.addWidget(entry2)
self.setLayout(v_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())
水平布局管理器QHBoxLayout()

import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel("username:", self)
entry1 = QLineEdit(self)
label2 = QLabel("password:", self)
entry2 = QLineEdit(self)
# 布局
h_layout = QHBoxLayout()
h_layout.addWidget(label1)
h_layout.addWidget(entry1)
h_layout.addWidget(label2)
h_layout.addWidget(entry2)
self.setLayout(h_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())
表單布局管理器QFormLayout()
常用方法
addRow(label, widget): 添加一行,包含標(biāo)簽和控件。例如用戶名輸入框或密碼輸入框。setFormAlignment(): 設(shè)置整個(gè)表單的對齊方式。setLabelAlignment(): 設(shè)置標(biāo)簽的對齊方式。addWidget(widget): 添加一個(gè)占用整行的控件,比如按鈕或描述文字。
import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel("username:", self)
entry1 = QLineEdit(self)
label2 = QLabel("password:", self)
entry2 = QLineEdit(self)
# 布局
f_layout = QFormLayout()
f_layout.addRow(label1import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QFormLayout
from PyQt6.QtCore import Qt
class FormExample(QWidget):
def __init__(self):
super().__init__()
# 設(shè)置窗口標(biāo)題和大小
self.setWindowTitle("Form Alignment Example")
self.resize(400, 200)
# 創(chuàng)建表單布局
form_layout = QFormLayout()
# 添加標(biāo)簽和輸入框
form_layout.addRow("Username:", QLineEdit())
form_layout.addRow("Password:", QLineEdit())
# 添加按鈕
login_button = QPushButton("Login")
form_layout.addWidget(login_button)
# 設(shè)置表單整體對齊方式
form_layout.setFormAlignment(Qt.AlignmentFlag.AlignCenter) # 整體居中
# 設(shè)置標(biāo)簽對齊方式
form_layout.setLabelAlignment(Qt.AlignmentFlag.AlignRight) # 標(biāo)簽右對齊
# 應(yīng)用布局
self.setLayout(form_layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = FormExample()
w.show()
sys.exit(app.exec())
, entry1)
f_layout.addRow(label2, entry2)
self.setLayout(f_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())
網(wǎng)格布局管理器QGridLayout()
類似Tk的grid()布局方式
import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel("username:", self)
entry1 = QLineEdit(self)
label2 = QLabel("password:", self)
entry2 = QLineEdit(self)
# 布局
grid_layout = QGridLayout()
grid_layout.addWidget(label1,0,0)
grid_layout.addWidget(entry1,0,1)
grid_layout.addWidget(label2,1,0)
grid_layout.addWidget(entry2,1,1)
# 應(yīng)用布局
self.setLayout(grid_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())

布局嵌套
布局管理器除了可以添加控件,還可以添加子布局
import sys
from PyQt6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
label1 = QLabel("username:", self)
entry1 = QLineEdit(self)
label2 = QLabel("password:", self)
entry2 = QLineEdit(self)
# 布局
v_layout = QVBoxLayout()
h1_layout = QHBoxLayout()
h1_layout.addWidget(label1)
h1_layout.addWidget(entry1)
h2_layout = QHBoxLayout()
h2_layout.addWidget(label2)
h2_layout.addWidget(entry2)
v_layout.addLayout(h1_layout)
v_layout.addLayout(h2_layout)
# 應(yīng)用布局
self.setLayout(v_layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec())

到此這篇關(guān)于PyQt6 布局管理器的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyQt6 布局管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PyQt5每天必學(xué)之布局管理
- 淺談pyqt5在QMainWindow中布局的問題
- PyQt Qt Designer工具的布局管理詳解
- Pyqt5自適應(yīng)布局實(shí)例
- python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細(xì)使用方法
- python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細(xì)使用方法與實(shí)例
- python GUI庫圖形界面開發(fā)之PyQt5動態(tài)(可拖動控件大小)布局控件QSplitter詳細(xì)使用方法與實(shí)例
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
- Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5布局管理項(xiàng)目示例詳解
相關(guān)文章
使用python在校內(nèi)發(fā)人人網(wǎng)狀態(tài)(人人網(wǎng)看狀態(tài))
人人網(wǎng)怎么發(fā)狀態(tài)?下面使用python實(shí)現(xiàn)這個(gè)功能,大家參考使用吧2014-02-02
python3 實(shí)現(xiàn)在運(yùn)行的時(shí)候隱藏命令窗口
這篇文章主要介紹了python3 實(shí)現(xiàn)在運(yùn)行的時(shí)候隱藏命令窗口方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python實(shí)現(xiàn)sqlalchemy的使用概述
SQLAlchemy是Python中最有名的ORM工具,特點(diǎn)是操縱Python對象而不是SQL查詢,也就是在代碼層面考慮的是對象,而不是SQL,體現(xiàn)的是一種程序化思維,這樣使得Python程序更加簡潔易懂,具體內(nèi)容詳情跟隨小編一起看看吧2021-08-08
python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
python入門字符串拼接\截取\轉(zhuǎn)數(shù)字理解學(xué)習(xí)
本篇內(nèi)容我們主要講有關(guān)Python字符串的用法,包括字符串的拼接、字符串怎么轉(zhuǎn)數(shù)字、字符串的格式化、字符串函數(shù)等內(nèi)容,有需要的朋友可以借鑒參考下2021-09-09
python實(shí)現(xiàn)合并兩個(gè)排序的鏈表
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)合并兩個(gè)排序的鏈表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
解決Python requests 報(bào)錯(cuò)方法集錦
這篇文章主要介紹了解決Python requests 報(bào)錯(cuò)方法集錦的相關(guān)資料,需要的朋友可以參考下2017-03-03

