Pyqt5 布局和滚动条 学习笔记

  • Post author:
  • Post category:其他


一、效果展示

(1)界面较大时,显示所东西

(2)界面缩放时,自动出现卷轴

二、代码解析

1、窗口的布局实现

# ...
        # 1 例化一个widgets, 作为窗口的主要显示内容
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        # 2 设置窗口中心内容的布局工具
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralWidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        # 3 设置窗口中心内容的分割工具
        self.splitter = QtWidgets.QSplitter(self.centralWidget)
        self.splitter.setOrientation(QtCore.Qt.Vertical)
        self.splitter.setObjectName("splitter")
        # 4 添加内容到==>分割工具
        self.tabWidget = QtWidgets.QTabWidget(self.splitter)
        self.textEdit = QtWidgets.QTextEdit(self.splitter)
        # 5 将分割工具添加到==>布局工具 
        self.horizontalLayout.addWidget(self.splitter)
        # 6 设置窗体的中心显示内容
        MainWindow.setCentralWidget(self.centralWidget)
# ...

2、多标签页tabWidget的实现

# ...
        # 1  生成多标签页的顶层组件
        self.tabWidget = QtWidgets.QTabWidget(self.splitter)
        self.tabWidget.setEnabled(True)
        # 2 每页的tab具体内容可以是widget类型组件
        self.tab_2 = QtWidgets.QWidget()
        # 3 将每页的widget添加到 ==> tabWidget 
        self.tabWidget.addTab(self.tab_one, "")
        self.tabWidget.addTab(self.tab_2, "")
# ...

3、单标签页widget组件的实现

# ...
        # 1 生成一个widget组件的实例
        self.tab_one = QtWidgets.QWidget()
        # 2 设置widget的布局工具
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.tab_one)
        # 3 设置widget的分割工具
        self.splitter_tab_one = QtWidgets.QSplitter(self.tab_one)
        # 4 将内容添加到==> 分割工具
        self.widget_btn = QtWidgets.QWidget(self.splitter_tab_one)
        self.widget_tool = QtWidgets.QWidget(self.splitter_tab_one)
        # 5 将分割工具添加到==> 布局工具
        self.horizontalLayout_2.addWidget(self.splitter_tab_one)
# ...

4、单标签页中设置自动滚动条工具

# ...
        # 1 实例化一个组件
        self.widget_btn = QtWidgets.QWidget(self.splitter_tab_one)
        self.widget_btn.setObjectName("widget_btn")
        # 2 设置组件的布局方式
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget_btn)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        # 3 设置组件的滚动区域
        self.scrollArea = QtWidgets.QScrollArea(self.widget_btn)
        self.scrollArea.setWidgetResizable(True)
        # 4 设置滚动区域的内容组件
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        # 5 设置滚动区内容的布局方式
        self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
        # 6 将按钮添加到==>滚轴区布局方式
        self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.verticalLayout.addWidget(self.pushButton)
        # 6 将按钮添加到==>滚轴区布局方式
        self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.verticalLayout.addWidget(self.pushButton_2)
        # 6 将按钮添加到==>滚轴区布局方式
        self.pushButton_3 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.verticalLayout.addWidget(self.pushButton_3)
        # 7 设置滚动区域显示内容
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        # 8 将滚动区域添加到布局管理器
        self.horizontalLayout_3.addWidget(self.scrollArea)
# ...

5、留个位置为做个总结

(1)

(2)

(3)

三、代码原文

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'D:\Users\WorkSpace\Pyqt5_eric6\hello_main.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(654, 771)

        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")

        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralWidget)
        self.horizontalLayout.setObjectName("horizontalLayout")

        self.splitter = QtWidgets.QSplitter(self.centralWidget)
        self.splitter.setOrientation(QtCore.Qt.Vertical)
        self.splitter.setObjectName("splitter")

        self.tabWidget = QtWidgets.QTabWidget(self.splitter)
        self.tabWidget.setEnabled(True)
        self.tabWidget.setMinimumSize(QtCore.QSize(0, 85))
        self.tabWidget.setObjectName("tabWidget")

        self.tab_one = QtWidgets.QWidget()
        self.tab_one.setMinimumSize(QtCore.QSize(0, 0))
        self.tab_one.setObjectName("tab_one")

        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.tab_one)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")

        self.splitter_tab_one = QtWidgets.QSplitter(self.tab_one)
        self.splitter_tab_one.setOrientation(QtCore.Qt.Horizontal)
        self.splitter_tab_one.setObjectName("splitter_tab_one")

        self.widget_btn = QtWidgets.QWidget(self.splitter_tab_one)
        self.widget_btn.setObjectName("widget_btn")

        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget_btn)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")

        self.scrollArea = QtWidgets.QScrollArea(self.widget_btn)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")

        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 284, 362))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

        self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
        self.verticalLayout.setObjectName("verticalLayout")

        self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)

        self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)

        self.pushButton_3 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton_3.setObjectName("pushButton_3")
        self.verticalLayout.addWidget(self.pushButton_3)

        self.pushButton_4 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton_4.setObjectName("pushButton_4")
        self.verticalLayout.addWidget(self.pushButton_4)

        self.pushButton_5 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton_5.setObjectName("pushButton_5")
        self.verticalLayout.addWidget(self.pushButton_5)

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.horizontalLayout_3.addWidget(self.scrollArea)

        self.widget_tool = QtWidgets.QWidget(self.splitter_tab_one)
        self.widget_tool.setObjectName("widget_tool")
        self.horizontalLayout_2.addWidget(self.splitter_tab_one)

        self.tabWidget.addTab(self.tab_one, "")

        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")

        self.textEdit = QtWidgets.QTextEdit(self.splitter)
        self.textEdit.setMaximumSize(QtCore.QSize(16777215, 300))
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout.addWidget(self.splitter)

        MainWindow.setCentralWidget(self.centralWidget)
        
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 654, 23))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton_1"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
        self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_one), _translate("MainWindow", "Tab 1"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))


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_())



版权声明:本文为ShiAokai原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。