PyQt滚动条:
from PyQt5 import QtCore,QtGui,QtWidgets
class UI_MainWindow(object):
def setupUI(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800,600)
# 中央控件
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(70,30,620,460))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
# 垂直布局
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0,0,0,0)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.scrollArea = QtWidgets.QScrollArea(self.verticalLayoutWidget)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setObjectName("scrollArea")
self.button_name = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0,0,800,len(self.button_name) * 80))
self.scrollAreaWidgetContents.setObjectName("self.scrollAreaWidgetContents")
# 循环按钮
for i in range(len(self.button_name)):
self.pushButton = QtWidgets.QPushButton(self.ascrollAreaWidgetContents)
#
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(25)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMinimumSize(QtCore.QSize(600,60))
self.pushButton.setMaximumSize(QtCore.QSize(16777215,10100))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
#
self.pushButton.setObjectName("pushButton" + str(i))
self.pushButton.setText(self.button_name[i])
self.pushButton.move(30,80 * i)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout.addWidget(self.scrollArea)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUI(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUI(self):
_translate = QtCore.QCoreApplication.translate
self.label.setText(_translate("MainWindow", "滑动条设计:"))
if __name__ =="__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = UI_MainWindow()
ui.setupUI(MainWindow)
MainWindow.show()
sys.exit(app.exec_())