本文共 3921 字,大约阅读时间需要 13 分钟。
PyQt5布局有两种方式,绝对定位和布局类
import sysfrom PyQt5.QtWidgets import QWidget, QLabel, QApplicationclass Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): lbl1 = QLabel('Zetcode', self) lbl1.move(15, 10) lbl2 = QLabel('tutorials', self) lbl2.move(15, 70) #绝对位置(35, 10) lbl3 = QLabel('for programmers', self) lbl3.move(70, 70) #设置窗口位置和大小 self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Absolute Positioning') self.show()if __name__ == '__main__': app = QApplication(sys.argv) execute = Example() sys.exit(app.exec_())使用QHBoxLayout和QVBoxLayout,来分别创建横向布局和纵向布局
import sysfrom PyQt5.QtWidgets import QWidget, QLabel, QApplicationimport sysfrom PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication)class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): okButton = QPushButton("OK1") cancelButton = QPushButton("Cancel") okButton1 = QPushButton("OK2") cancelButton1 = QPushButton("Cance2") hbox1 = QHBoxLayout() #添加横向伸展因子,靠右显示 hbox1.addStretch(1) hbox1.addWidget(okButton) hbox1.addWidget(cancelButton) hbox2 = QHBoxLayout() #hbox2.addStretch(1) hbox2.addWidget(okButton1) hbox2.addWidget(cancelButton1) vbox = QVBoxLayout() #添加纵向伸展因子,靠底部显示 vbox.addStretch(1) vbox.addLayout(hbox1) vbox.addLayout(hbox2) self.setLayout(vbox) self.setGeometry(300, 300, 300, 150) self.setWindowTitle('Buttons') self.show()if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())使用QGridLayout类创建一个网格布局
import sysfrom PyQt5.QtWidgets import (QWidget, QGridLayout, QPushButton, QApplication)class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): #创建一个网格 grid = QGridLayout() self.setLayout(grid) #按钮的标签 names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+'] positions = [(i, j) for i in range(5) for j in range(4)] for position, name in zip(positions, names): if name == '': #标签为空时,不创建按钮 continue button = QPushButton(name) grid.addWidget(button, *position) self.move(300, 150) self.setWindowTitle('Calculator') self.show()if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())文本编辑框
import sysfrom PyQt5.QtWidgets import (QWidget, QGridLayout, QPushButton, QApplication,QTextEdit)class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): #创建一个网格 grid = QGridLayout() self.setLayout(grid) text_edit = QTextEdit() #部件的行和列跨,这里的跨度为1行,4列 grid.addWidget(text_edit,0,0,1,4) #组件的间隔 #grid.setSpacing(5) #按钮的标签 names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+'] positions = [(i, j) for i in range(1,6) for j in range(4)] for position, name in zip(positions, names): if name == '': #标签为空时,不创建按钮 continue button = QPushButton(name) grid.addWidget(button, *position) self.move(300, 150) self.setWindowTitle('Calculator') self.show()if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
转载地址:http://oaben.baihongyu.com/