P95 绝对布局
绝对布局,使用move方法,操作坐标来控件控件的位置。
import sys
from PyQt5.QtWidgets import *
'''
绝对布局,使用move方法,操作坐标来控件控件的位置。
'''
class MyWin(QWidget):def __init__(self):super().__init__()# self.resize(300, 300)self.setWindowTitle('绝对布局')self.label1=QLabel("欢迎",self)self.label1.move(15,20)self.label2=QLabel("学习",self)self.label2.move(35,40)self.label3=QLabel("PyQt5",self)self.label3.move(55,80)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P96 水平盒布局
学习的新方法:
hlay.setSpacing() 设置控件间距,参数为数字
课堂练习代码:
import sys
from PyQt5.QtWidgets import *class MyWin(QWidget):def __init__(self):super().__init__()hlay=QHBoxLayout()hlay.setSpacing(40) # 设置控件间距hlay.addWidget(QPushButton("按钮1"))hlay.addWidget(QPushButton("按钮2"))hlay.addWidget(QPushButton("按钮3"))hlay.addWidget(QPushButton("按钮4"))hlay.addWidget(QPushButton("按钮5"))self.setLayout(hlay)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P97设置控件拉伸因子及对齐方式
参数:
addWidget(self, QWidget, stretch=0, alignment, Qt_Alignment=None, Qt_AlignmentFlag=None, *args, **kwargs)参数1.QWidget 表示控件,比如按钮等。参数2.stretch 参数值为数字,比如代码中的数字2或4,数字表示拉伸因子:在 PyQt5 中,Hlay.addWidget(假设应该是水平布局QHBoxLayout的addWidget方法)中的数字参数通常是拉伸因子(stretch factor)。这个参数决定了在布局调整大小时,各个添加到布局中的部件如何分配多余的空间。例如,如果有多个部件添加到一个水平布局中,并且给不同的部件设置不同的拉伸因子,当布局的宽度发生变化时,具有较大拉伸因子的部件会分配到更多的额外空间,而拉伸因子较小的部件分配到的额外空间相对较少。如果不设置这个参数或者设置为 0,部件通常不会自动拉伸以适应布局的变化。参数3.Qt_Alignment 表示对齐方式,参数值比如为Qt.AlignLeft | Qt.AlignBottom,同时有两种对齐方式用|连接。
课堂练习代码:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()hlay=QHBoxLayout()# hlay.setSpacing(40) # 设置控件间距hlay.addWidget(QPushButton("按钮1"),2,Qt.AlignLeft | Qt.AlignTop)hlay.addWidget(QPushButton("按钮2"),4,Qt.AlignLeft | Qt.AlignTop)hlay.addWidget(QPushButton("按钮3"),1,Qt.AlignLeft | Qt.AlignTop)hlay.addWidget(QPushButton("按钮4"),1,Qt.AlignLeft | Qt.AlignBottom)hlay.addWidget(QPushButton("按钮5"),1,Qt.AlignLeft | Qt.AlignBottom)self.setLayout(hlay)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P98 垂直盒布局
课堂练习代码:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()Vlay=QVBoxLayout()# hlay.setSpacing(40) # 设置控件间距Vlay.addWidget(QPushButton("按钮1"))Vlay.addWidget(QPushButton("按钮2"))Vlay.addWidget(QPushButton("按钮3"))Vlay.addWidget(QPushButton("按钮4"))Vlay.addWidget(QPushButton("按钮5"))self.setLayout(Vlay)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P99 设置布局的伸缩量(addStretch用法,好好总结一下该知识点)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('设置伸缩量')self.resize(800,100)b1=QPushButton(self)b2=QPushButton(self)b3=QPushButton(self)b4=QPushButton(self)b5=QPushButton(self)b1.setText("按钮1")b2.setText("按钮2")b3.setText("按钮3")b4.setText("按钮4")b5.setText("按钮5")lay=QHBoxLayout()lay.addStretch(0)lay.addWidget(b1)lay.addWidget(b2)lay.addWidget(b3)lay.addWidget(b4)lay.addWidget(b5)lay.addStretch(1)btOK=QPushButton("确定")lay.addWidget(btOK)btCancel=QPushButton("取消")lay.addWidget(btCancel)self.setLayout(lay)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P100让按钮永远位于右下角
1.应用场景
2.课堂练习代码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('让按钮永远位于右下角')self.resize(400,300)hlay=QHBoxLayout()btOK=QPushButton("确定")btCancel=QPushButton("取消")hlay.addStretch(1)hlay.addWidget(btOK)hlay.addWidget(btCancel)vlay=QVBoxLayout()bt1=QPushButton("按钮1")bt2=QPushButton("按钮2")bt3=QPushButton("按钮3")vlay.addStretch(1)vlay.addWidget(bt1)vlay.addWidget(bt2)vlay.addWidget(bt3)vlay.addLayout(hlay)self.setLayout(vlay)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P101 网格布局:用循环方式实现计算器UI
课堂练习代码(优于老师的代码):
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('网格布局')lay=QGridLayout()self.setLayout(lay)names=['Cls','Back','','Close','7','8','9','/','4','5','6','*','1','2','3','-','0','.','=','+']for num in range(len(names)):row=num//4column=num%4lay.addWidget(QPushButton(names[num]),row,column)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P102 网格布局:进行表单UI设计(使用跨行跨列功能)
1.设计效果
2.课堂练习代码:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('网格布局:表单设计')lay=QGridLayout()self.setLayout(lay)titleLabel=QLabel('标题')authorLabel=QLabel('作者')contentLabel=QLabel('内容')titleEdit=QLineEdit()authorEidt=QLineEdit()contentEdit=QTextEdit()lay.addWidget(titleLabel,1,0)lay.addWidget(titleEdit,1,1)lay.addWidget(authorLabel,2,0)lay.addWidget(authorEidt,2,1)lay.addWidget(contentLabel,3,0)lay.addWidget(contentEdit,3,1,5,1) # 网格布局中跨行跨列设置if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P103表单布局(放大时TextEdittd行UI效果优于P102实现效果)
1.实现效果
2.课堂练习代码:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('表单布局')self.resize(350,300)lay=QFormLayout()self.setLayout(lay)titleLabel=QLabel('标题')authorLabel=QLabel('作者')contentLabel=QLabel('内容')titleEdit=QLineEdit()authorEidt=QLineEdit()contentEdit=QTextEdit()lay.addRow(titleLabel,titleEdit)lay.addRow(authorLabel,authorEidt)lay.addRow(contentLabel,contentEdit)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()
P104 拖动控件之间的边界(QSplitter未听明白其作用)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyWin(QWidget):def __init__(self):super().__init__()self.setWindowTitle('QSplitter例子')self.setGeometry(300,300,300,200)topleft=QFrame()topleft.setFrameShape(QFrame.StyledPanel)bottom=QFrame()bottom.setFrameShape(QFrame.StyledPanel)splitter1 = QSplitter(Qt.Horizontal)edit = QTextEdit()splitter1.addWidget(topleft)splitter1.addWidget(edit)splitter1.setSizes([100,200]) # 设置尺寸splitter2 = QSplitter(Qt.Vertical)splitter2.addWidget(splitter1)splitter2.addWidget(bottom)hbox=QHBoxLayout()hbox.addWidget(splitter2)self.setLayout(hbox)if __name__ == '__main__':app = QApplication(sys.argv)w = MyWin()w.show()app.exec()