PyQt5的QScrollArea组件为一种控件容器,它的的作用是可以容纳更多的组件,如果组件过多会出现滚动条,滚动条会根据容器的大小与内部组件的大小变化。
使用方法可以参考如下实例
import sys
from PyQt5.QtWidgets import *
class test(QWidget):
def __init__(self):
super().__init__()
self.initui()
def initui(self):
la=QHBoxLayout()
lb=QVBoxLayout()
lc=QHBoxLayout()
scroll=QScrollArea()
a=QWidget()
a.setLayout(lb)
lb.addLayout(lc)
for x in range(50):
lb.addWidget(QPushButton(str(x)))
for x in range(50):
lc.addWidget(QPushButton(str(x)))
scroll.setMinimumSize(400,400)
#scrollarea 作为一个组件,可以设置窗口
scroll.setWidget(a)
la.addWidget(scroll)
self.setLayout(la)
self.show()
if __name__=='__main__':
app=QApplication(sys.argv)
win=test()
sys.exit(app.exec_())
运行结果为
本文通过实例代码介绍了PyQt5中QScrollArea组件的使用方法,该组件作为容器可容纳多个组件,并在内容过多时自动显示滚动条。示例中创建了一个包含两个垂直布局的窗口,每个布局填充了50个按钮,当窗口大小调整时,滚动条会根据需要出现。
257

被折叠的 条评论
为什么被折叠?



