有一个以表格形式放置组件的窗口QGridLayout。
由于使用了组件QLabel,它们可以任意数量的水平和垂直放置。
例如20 x 20,结果是400,一般情况下,会有多少组件是事先不知道的,这就是为什么需要在窗口中沿两个轴滚动的原因。
如何添加水平和垂直滚动的窗口?
这是一个示例代码:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.grid = QGridLayout()
self.setLayout(self.grid)
self.addComponents()
self.show()
def addComponents(self):
i = 0
j = 0
countLab = 0
while i < 25:
while j < 25:
lab = QLabel('lab_'+ str(countLab))
self.layout().addWidget(lab, i, j)
countLab += 1
j += 1
j = 0
i += 1
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setWindowTitle('Example')
sys.exit(app.exec_())
作为一种选择:
选项 2
使界面可拉伸: