我已经在pyside 6中创建了Windows窗体. 它有一个主框架和一个控制台框架. 有一个用于移动控制台框架(向上和向下)的拆分器和一个(切换)按钮.

当我点击切换按钮时,显示控制台框,如果我再次点击,它的 隐藏框架.

这是设计部分:

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QHBoxLayout, QMainWindow,
    QPushButton, QSizePolicy, QSplitter, QTextEdit,
    QVBoxLayout, QWidget)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")

        MainWindow.setEnabled(True)
        MainWindow.resize(1234, 905)
        MainWindow.setStyleSheet(u"background-color: rgb(58, 58, 102);")
        MainWindow.setUnifiedTitleAndToolBarOnMac(True)

        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")

        self.verticalLayout = QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(u"verticalLayout")

        self.splitter = QSplitter(self.centralwidget)
        self.splitter.setObjectName(u"splitter")
        self.splitter.setMinimumSize(QSize(0, 4))
        self.splitter.setStyleSheet(u"")
        self.splitter.setOrientation(Qt.Vertical)

        self.master_frame = QFrame(self.splitter)
        self.master_frame.setObjectName(u"master_frame")

        sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.master_frame.sizePolicy().hasHeightForWidth())

        self.master_frame.setSizePolicy(sizePolicy)
        self.master_frame.setMaximumSize(QSize(16777215, 16777215))
        self.master_frame.setFrameShape(QFrame.StyledPanel)
        self.master_frame.setFrameShadow(QFrame.Raised)

        self.horizontalLayout = QHBoxLayout(self.master_frame)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.horizontalLayout.setContentsMargins(0, -1, 0, 0)

        self.visual_frame = QFrame(self.master_frame)
        self.visual_frame.setObjectName(u"visual_frame")
        self.visual_frame.setFrameShape(QFrame.StyledPanel)
        self.visual_frame.setFrameShadow(QFrame.Raised)

        self.horizontalLayout_2 = QHBoxLayout(self.visual_frame)
        self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")

        self.textEdit = QTextEdit(self.visual_frame)
        self.textEdit.setObjectName(u"textEdit")
        self.textEdit.setStyleSheet(u"background-color: rgb(52, 52, 52); color: rgb(255, 255, 255);")

        self.horizontalLayout_2.addWidget(self.textEdit)

        self.horizontalLayout.addWidget(self.visual_frame)

        self.ctrl_frame = QFrame(self.master_frame)
        self.ctrl_frame.setObjectName(u"ctrl_frame")
        self.ctrl_frame.setMinimumSize(QSize(250, 0))
        self.ctrl_frame.setMaximumSize(QSize(250, 16777215))
        self.ctrl_frame.setFrameShape(QFrame.StyledPanel)
        self.ctrl_frame.setFrameShadow(QFrame.Raised)

        self.btn_toggle = QPushButton(self.ctrl_frame)
        self.btn_toggle.setObjectName(u"btn_toggle")
        self.btn_toggle.setGeometry(QRect(40, 450, 171, 81))

        font = QFont()
        font.setPointSize(19)

        self.btn_toggle.setFont(font)
        self.btn_toggle.setCursor(QCursor(Qt.ArrowCursor))
        self.btn_toggle.setStyleSheet(u"color: rgb(255, 255, 255);")

        self.horizontalLayout.addWidget(self.ctrl_frame)

        self.splitter.addWidget(self.master_frame)

        self.console_frame = QFrame(self.splitter)
        self.console_frame.setObjectName(u"console_frame")

        sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        sizePolicy1.setHorizontalStretch(0)
        sizePolicy1.setVerticalStretch(0)
        sizePolicy1.setHeightForWidth(self.console_frame.sizePolicy().hasHeightForWidth())

        self.console_frame.setSizePolicy(sizePolicy1)
        self.console_frame.setMinimumSize(QSize(0, 0))
        self.console_frame.setMaximumSize(QSize(16777215, 16777215))
        self.console_frame.setBaseSize(QSize(0, 0))
        self.console_frame.setFrameShape(QFrame.StyledPanel)
        self.console_frame.setFrameShadow(QFrame.Raised)

        self.verticalLayout_4 = QVBoxLayout(self.console_frame)
        self.verticalLayout_4.setObjectName(u"verticalLayout_4")
        self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)

        self.textEdit_console = QTextEdit(self.console_frame)
        self.textEdit_console.setObjectName(u"textEdit_console")
        self.textEdit_console.setMinimumSize(QSize(0, 0))

        font1 = QFont()
        font1.setPointSize(12)

        self.textEdit_console.setFont(font1)
        self.textEdit_console.viewport().setProperty("cursor", QCursor(Qt.IBeamCursor))
        self.textEdit_console.setStyleSheet(u"background-color: #282C34; color: rgb(0, 255, 0);")

        self.verticalLayout_4.addWidget(self.textEdit_console)

        self.splitter.addWidget(self.console_frame)

        self.verticalLayout.addWidget(self.splitter)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"Toggle Frame", None))
        self.btn_toggle.setText(QCoreApplication.translate("MainWindow", u"Toggle", None))
    # retranslateUi

这是函数类:

from PySide6.QtWidgets import QMainWindow


class UIFunctions(QMainWindow):
    def __init__(self, ui_main_window):
        super(UIFunctions, self).__init__()

        self.oldPosition = None
        self.ui = ui_main_window
        self.ui.setupUi(self)

        # Console Resize initial
        self.setup_console_resize_button()

    def setup_console_resize_button(self):
        self.ui.btn_toggle.clicked.connect(self.toggle_console_height)

    def toggle_console_height(self):
        console_frame = self.ui.console_frame
        current_height = console_frame.height()

        if current_height == 0:
            # If the console is hidden, calculate the new height
            new_height = 220
            self.ui.master_frame.setMaximumHeight(self.ui.master_frame.maximumHeight() + new_height)
        else:
            # If the console is visible, set the new height to 0
            new_height = 0
            self.ui.master_frame.setMaximumHeight(self.ui.master_frame.maximumHeight() - current_height)

        # Set the size of the console frame explicitly
        console_frame.setMinimumHeight(new_height)
        console_frame.setMaximumHeight(new_height)

        # Calculate splitter sizes
        total_height = self.ui.master_frame.maximumHeight() + self.ui.console_frame.maximumHeight()
        master_frame_ratio = self.ui.master_frame.maximumHeight() / total_height
        console_frame_ratio = self.ui.console_frame.maximumHeight() / total_height

        # Set splitter sizes
        self.ui.splitter.setSizes([int(master_frame_ratio * total_height), int(console_frame_ratio * total_height)])

主要方法:

import sys
import platform
import PySide6
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect,
                            QSize, QTime, QUrl, Qt, QEvent)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence,
                           QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PySide6.QtWidgets import *
from PySide6.QtWidgets import QApplication

from ui_design import Ui_MainWindow
from ui_functions import UIFunctions


class MainWindow(UIFunctions):
    def __init__(self):
        super(MainWindow, self).__init__(Ui_MainWindow())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

但有一次,如果我点击切换按钮,那么拆分器就不能正常工作了!

你能解决这个问题吗?

推荐答案

拆分器只能调整与其方向一致且在小部件大小限制范围内的尺寸:其最小和最大长度.如果拆分器是水平的,它只能在minimumWidth()maximumWidth()之间调整小部件的大小,最终考虑在折叠的情况下调整小部件的minimumSizeHint()(当小部件太小时隐藏小部件,或者如果有足够的空间重新显示它).

那个QSplter不再起作用了,因为做setMinimumHeight(new_height)setMaximumHeight(new_height)就像做setFixedHeight(new_height)一样,而vertical Split不能改变那个高度.

Since you're also setting the item sizes of the splitter, you should only set the maximum height of the "console", not the minimum (which is implicitly 0). Also note that you should not set an arbitrary maximum height unless one was previously set (which doesn't seem the case), otherwise you would limit the possibility to expand it after toggling, even if it was previously possible.
You should instead restore the default maximum height (QtWidgets.QWIDGETSIZE_MAX, 16777215) and properly compute the ratios using the currently suggested height, not the maximum, and based on the current height of the splitter container.

Besides, in reality, all this is quite pointless.
If all you want is to toggle the visibility of a widget, just use the proper function, which is exactly what QSplitter normally does, and is achieved by calling the basic setVisible() QWidget function, against a negation of the current value of that same property:

    def toggle_console_height(self):
        self.ui.console_frame.setVisible(
            not self.ui.console_frame.isVisible()
        )

并删除所有剩余的代码,这是不必要的.

Python-3.x相关问答推荐

使用Python装载. iso文件

循环遍历数据框以提取特定值

我正在try 从 10*3 矩阵中删除随机值并将其变为 10*2 矩阵

CSV-DAT 转换时将引号添加到数据中

三重奏:为什么频道被记录为使用async with,而不是with?

如何在类中的函数(以 self 作为第一个参数)中使用递归

单击图形时 plotly graph_objects 持久性数据

当我判断另一个 checkButton 时,如何判断两个 python tkinter checkButtons?

合并两个numpy数组

django rest框架中的save()、create()和update()有什么区别?

Python:pprint的模块错误,打印没有错误

如何使我的课程在 Python 中非常可打印?

全局捕获快速 api 中的异常

django - 值更改后自动更新日期

如何通过命令行将数组传递给python

如何调试垂死的 Jupyter Python3 内核?

使用 Python3 与 HDFS 交互的最佳模块是什么?

try 在 Windows 10 高 DPI 显示器上解决模糊的 tkinter 文本 + zoom ,但担心我的方法不是 Pythonic 或不安全

为什么 Python 不能识别我的 utf-8 编码源文件?

是否可以在每个路由的基础上限制 Flask POST 数据大小?