文章目录
Pyside6布局管理
使用move()方法布局
锚点位于左上角,向右为x轴正方向,向下为y轴正方向
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
label_1=QLabel('label 1',self)
label_2=QLabel('label 2',self)
label_1.move(20,0)
label_2.move(50,100)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
控件较多时,该方法不便
坐标是固定的,当拉伸时不能自适应
垂直布局管理器QVBoxLayout
控件从上到下垂直进行摆放
先添加的控件位于后添加的控件上方
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
username=QLabel('username:')
password=QLabel('password:')
v_layout=QVBoxLayout()
v_layout.addWidget(username)
v_layout.addWidget(password)
#将垂直布局方式设置为窗口的整体布局
self.setLayout(v_layout)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
水平布局管理器QHBoxLayout
先添加的控件位于后添加的控件左侧
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
username_label=QLabel('username:')
username_line=QLineEdit()
h_layout=QHBoxLayout()
h_layout.addWidget(username_label)
h_layout.addWidget(username_line)
#将水平布局方式设置为窗口的整体布局
self.setLayout(h_layout)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
表单布局管理器QFormLayout
表单布局管理器指将控件按照表单的样式进行布局
通过调用addRow()方法进行添加控件
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
username_label=QLabel('username:')
password_label=QLabel('password')
username_line=QLineEdit()
password_line=QLineEdit()
f_layout=QFormLayout()
f_layout.addRow(username_label,username_line)
f_layout.addRow(password_label,password_line)
#将网格布局方式设置为窗口的整体布局
self.setLayout(f_layout)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
网格布局管理器QGridLayout
把窗口想象成带有网格的,这些网格都有相应坐标
网格坐标只在网格布局中使用
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
username_label=QLabel('username:')
password_label=QLabel('password')
username_line=QLineEdit()
password_line=QLineEdit()
g_layout=QGridLayout()
g_layout.addWidget(username_label,0,0)
g_layout.addWidget(username_line,0,1)
g_layout.addWidget(password_label,1,0)
g_layout.addWidget(password_line,1,1)
#将网格布局方式设置为窗口的整体布局
self.setLayout(g_layout)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())
布局嵌套
import sys
from PySide6.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
username_label=QLabel('username:')
password_label=QLabel('password:')
username_line=QLineEdit()
password_line=QLineEdit()
v_layout=QVBoxLayout()
h1_layout=QHBoxLayout()
h2_layout=QHBoxLayout()
h1_layout.addWidget(username_label)
h1_layout.addWidget(username_line)
h2_layout.addWidget(password_label)
h2_layout.addWidget(password_line)
v_layout.addLayout(h1_layout)
v_layout.addLayout(h2_layout)
self.setLayout(v_layout)
if __name__ == '__main__':
app=QApplication([])
window=Window()
window.show()
sys.exit(app.exec())