PyQt5布局大全

122 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

今天我们一起来看下 PyQt5的布局,我之前是做Android开发的在android里面也有布局这一说简单的来说,主要是

  • 1,线性布局
  • 2,相对布局
  • 3,表格布局
  • 4,帧布局
  • 5,绝对布局 其实在PyQt5中,这些都是类似的

1,PyQt5的绝对布局

这里写图片描述

import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
calss Example(QWidget):
	def __init__(self):
		super().__init__()
		self.initUI()
	
	def initUI(self):
		lbl1 = QLabel("你好", self)
		lbl1.move(15, 10)
	
		lbl2 = QLabel("python", self)
		lbl2.move(35,40)

		lbl3 = QLable("world", self)
		lbl3.move(55, 70)

		self.setGeometry(300, 300, 320, 120)
		self.setWindowTitle("绝对布局")
if __name__ == '__main__':  
	app = QApplication(sys.argv)
	demo = Example()
	demo.show()
	sys.exet(app.exec_())

2,PyQt5的线性布局之水平布局

这里写图片描述

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class WInform(QWidget):
	def __init__(self, parent=None):
		super(Windform, self).__init__(parent)
		
		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)))
		# 设置控件的间距
		hlayout.setSpaceing(0)
		self.setLayout(hlayout)
if __name__ == "__main__":
	app = QApplication(sys.argv)
	form = Winform()
	form.show()
	sys.exit(app.exec_())
			

3,PyQt5的线性布局之水平布局(二)

这里写图片描述

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("水平布局管理")
		self.resize(800,200)
		
		hlayout = QHbobLayout()
		#按钮水平居左,垂直居上
		hlayout.addWidget(QPushButton(str(1)),0, Qt.AlignLeft|Qt.AlignTop)
		hlayout.addWidget(QPushButton(str(2)),0, Qt.AlignLeft|Qt.AlignTop)
		hlayout.addWidget(QPushButton(str(3),5))#这里5表示的是权重
		#水平居左,垂直居下
		hlayout.addWidget(QPushButton(str(4)),0,Qt.AlignLeft|Qt.AlignBottom)
		hlayout.addWidget(QpushButton(str(5)),0, Qt.ALignLeft|Qt.AlignBottom)
		self.setLayout(hlayout)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())

4,PyQt5的线性布局之垂直布局

这里写图片描述

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QLineEdit, QLabel
class Winform(QWidget):
	def __init__(self, parent=None):
		super(Winform, self).__init__(parent)
		self.setWindowTitle("垂直布局")
		self.resize(400, 100)
		
		formLayout = QFormLayout()
		lab1 = QLable("标签1")
		lineEdit1 = QLineEdit()
		lab2 = QLable("标签2")
		lineEdit2 = QLineEdit()
		lab3 = QLable("标签3")
		lineEdit3 = QLineEdit()
		#向布局中添加这几个控件
		formLayout.addRow(labl1, lineEdit1)
		# 一行两个控件,水平摆放
		formLayout.addRow(labl2, lineEdit2)
		formLayout.addRow(lab3, lineEdit3)

		self.setLayout(fromLayout)
if  __name__ =="__main__":
	app = QApplication(sys.argv)
	form = Winform()
	form.show()
	sys.exit(app.exec_())
			

5,PyQt5的网格布局

这里写图片描述


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton
class Winform(QWidget):
	def __init__(self, parent=None):
		super(Winform, self).__init__(parent)
		self.initUI()
	
	def initUI(self):
		grid = QGridLayout()
		self.setLayout(grid)

		names = ['Cls', 'Back', '', 'Close',
                 '7', '8', '9', '/',
                 '4', '5', '6', '*',
                 '1', '2', '3', '-',
                 '0', '.', '=', '+']
        #使用生成器生成具体位置 5 列 4行
        positions = [(i,j) for i in range(5) for j in range(4)]
        for position, name in zip(position, names):
	        if name = "":
		        continue
		     button - QPushButton(name)
		     #Python允许你在list或tuple前面加一个*号,把list或tuple的元素变成可变参数传进去
		     grid.addwidget(button, *position)
		self.move(300, 150)
		self.setWindowTitle("网格布局管理")
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Winform()
    form.show()
    sys.exit(app.exec_())
        

6,PyQt5的网格布局(二)

这里写图片描述

class Winform(QWidget):
	def __init__(self, parent=None):
		super(Winform, self).__init__(parent)
		self.initUI()
	
	def initUI(self):
		titleLable = QLable("标题")
		authorLabel = QLabel('提交人')
        contentLabel = QLabel('申告内容')

		titleEdit = QLineEdit()
		authorEdit = QLineEdit()
        contentEdit = QTextEdit()
		grid = QGridLayout()
		grid.setSpacing(10)
		
		# 第一个参数表示控件,后面的两个参数表示的是控件所在的位置
		grid.addWidget(titleLable, 1, 0)
		grid.addWidget(titleEdit, 1, 1)
		
		grid.addWidget(authorLabel, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

		grid.addWidget(contentLable, 3,0)
		# 最后两个参数表示的是 所占的行和所占的列,开发者自己去体会吧
		grid.addWidget(gcontentEdit,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_())

不要复制,手动敲的,要理解其中的思想就行