一绝对位置
程序员以像素为单位指定每个小部件的位置和大小。 当您使用绝对定位时,我们必须了解以下限制:
- 如果我们调整窗口大小,则小部件的大小和位置不会改变
- 各种平台上的应用可能会有所不同
- 在我们的应用程序中更改字体可能会损坏布局
- 如果我们决定改变我们的布局,我们必须彻底重做布局,这很浪费时间
1、源码:
#coding = 'utf-8'
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.Init_UI()
def Init_UI(self):
self.setGeometry(300,300,400,300)
self.setWindowTitle('绝对布局')
bt1 = QPushButton('按钮一',self)
bt1.move(50,250)
bt2 = QPushButton('按钮二',self)
bt2.move(150,250)
bt3 = QPushButton('按钮三',self)
bt3.move(250,250)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
app.exit(app.exec_())
2、运行结果:
当你放大或者缩小窗口,三个按钮会静静地保持在原地。
二、箱式布局
QHBoxLayout和QVBoxLayout是基本的布局类,它们在水平和垂直方向上排列小部件。
想像一下,我们想在右下角放置三个按钮。 要创建这样一个布局,我们使用一个水平和一个垂直方框。 为了创建必要的空间,我们添加一个拉伸因子。(拉伸因子:指的是当窗口变化时,他会根据当前布局让你的空间跟随窗口大小而变化)
#coding = 'utf-8'
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QHBoxLayout, QVBoxLayout)
class Example(QWidget):
def __init__(self):
super().__init__()
self.Init_UI()
def Init_UI(self):
self.setGeometry(300,300,400,300)
self.setWindowTitle('箱式布局')
bt1 = QPushButton('按钮一', self)
bt2 = QPushButton('按钮二', self)
bt3 = QPushButton('按钮三', self)
hbox = QHBoxLayout() #创建一个水平箱子
hbox.addStretch(1) #添加一个因子,也可以用1,2,3等等,使用整倍数扩展
hbox.addWidget(bt1) #将按钮一添加到箱子
hbox.addStretch(1)
hbox.addWidget(bt2) #将按钮二添加到箱子
hbox.addStretch(1)
hbox.addWidget(bt3) #将按钮三添加到箱子
vbox = QVBoxLayout() #创建一个垂直箱子
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
app.exit(app.exec_())
2、运行结果
三、网络布局
1、源码:
#coding = 'utf-8'
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QGridLayout, QLCDNumber)
class Example(QWidget):
def __init__(self):
super().__init__()
self.Init_UI()
def Init_UI(self):
grid = QGridLayout()
self.setLayout(grid)
self.setGeometry(300,300,350,300)
self.setWindowTitle('计算器')
self.lcd = QLCDNumber()
grid.addWidget(self.lcd,1,0,1,8)
grid.setSpacing(10)
names = ['Cls', 'Bc', 'ret', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i,j) for i in range(4,9) for j in range(4,8)]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
grid.addWidget(button, *position)
button.clicked.connect(self.Cli)
self.show()
def Cli(self):
sender = self.sender().text()
ls = ['/', '*', '-', '=', '+']
if sender in ls:
self.lcd.display('A')
else:
self.lcd.display(sender)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
app.exit(app.exec_())
2、运行结果:
四、表单布局
一、源码
#coding = 'utf-8'
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QFormLayout, QLabel, QLineEdit, QTextEdit)
class Example(QWidget):
def __init__(self):
super().__init__()
self.Init_UI()
def Init_UI(self):
self.setGeometry(300,300,300,200)
self.setWindowTitle('表单布局')
formlayout = QFormLayout()
nameLabel = QLabel("姓名")
nameLineEdit = QLineEdit("")
introductionLabel = QLabel("简介")
introductionLineEdit = QTextEdit("")
formlayout.addRow(nameLabel,nameLineEdit)
formlayout.addRow(introductionLabel,introductionLineEdit)
self.setLayout(formlayout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
app.exit(app.exec_())
二、运行结果