PyQt6 布局管理器的六種實現(xiàn)示例
PyQt布局管理器有六種:
- move()方法布局
- 垂直布局管理器QVBoxLayout()
- 水平布局管理器QHBoxLayout()
- 表單布局管理器QFormLayout()
- 網(wǎng)格布局管理器QGridLayout()
- 布局嵌套
move()方法
根據(jù)窗口坐標布局,類似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 有點類似,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): 添加一行,包含標簽和控件。例如用戶名輸入框或密碼輸入框。setFormAlignment(): 設(shè)置整個表單的對齊方式。setLabelAlignment(): 設(shè)置標簽的對齊方式。addWidget(widget): 添加一個占用整行的控件,比如按鈕或描述文字。
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è)置窗口標題和大小
self.setWindowTitle("Form Alignment Example")
self.resize(400, 200)
# 創(chuàng)建表單布局
form_layout = QFormLayout()
# 添加標簽和輸入框
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è)置標簽對齊方式
form_layout.setLabelAlignment(Qt.AlignmentFlag.AlignRight) # 標簽右對齊
# 應(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 布局管理器的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyQt6 布局管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PyQt5每天必學之布局管理
- 淺談pyqt5在QMainWindow中布局的問題
- PyQt Qt Designer工具的布局管理詳解
- Pyqt5自適應(yīng)布局實例
- python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細使用方法
- python GUI庫圖形界面開發(fā)之PyQt5表單布局控件QFormLayout詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5動態(tài)(可拖動控件大小)布局控件QSplitter詳細使用方法與實例
- Python深度學習實戰(zhàn)PyQt5窗口切換的堆疊布局示例詳解
- Python深度學習實戰(zhàn)PyQt5布局管理項目示例詳解
相關(guān)文章
使用python在校內(nèi)發(fā)人人網(wǎng)狀態(tài)(人人網(wǎng)看狀態(tài))
人人網(wǎng)怎么發(fā)狀態(tài)?下面使用python實現(xiàn)這個功能,大家參考使用吧2014-02-02
python實現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
這篇文章主要為大家詳細介紹了python實現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
python入門字符串拼接\截取\轉(zhuǎn)數(shù)字理解學習
本篇內(nèi)容我們主要講有關(guān)Python字符串的用法,包括字符串的拼接、字符串怎么轉(zhuǎn)數(shù)字、字符串的格式化、字符串函數(shù)等內(nèi)容,有需要的朋友可以借鑒參考下2021-09-09

