我正在PySide6中编写一个程序,我创建了一个“窗帘”,当您按下按钮时它会改变其大小。
问题在于,当Dropdown()处于折叠位置时,按钮会相互重叠。
我们如何固定它们相对于窗口的位置,以便在最小高度时,只显示一个顶部按钮?
from PySide6.QtCore import Qt, QPropertyAnimation, Property, QEasingCurve
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QVBoxLayout,
QPushButton,
QFrame,
)
class Dropdown(QFrame):
def __init__(self):
super().__init__()
self.isMin = False
self.setFixedSize(300, 135)
self.setStyleSheet("background-color: white;")
button = QPushButton("Свернуть/развернуть")
button.setStyleSheet("background: red;")
button.setFixedHeight(25)
button.clicked.connect(self.anim)
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignmentFlag.AlignTop)
layout.addWidget(button)
for i in range(3):
btn = QPushButton(f"Button {i}")
btn.setFixedHeight(25)
btn.setStyleSheet("background-color: blue;")
layout.addWidget(btn)
self.setLayout(layout)
self.animation = QPropertyAnimation(self, b"Height")
self.animation.setDuration(300)
def anim(self):
if self.isMin:
self.animation.setStartValue(self.height())
self.animation.setEndValue(135)
self.isMin = False
else:
self.animation.setStartValue(self.height())
self.animation.setEndValue(40)
self.isMin = True
self.animation.start()
def setMinHeight(self, height):
self.setFixedHeight(height)
def getMinHeight(self):
return self.height()
Height = Property(int, getMinHeight, setMinHeight)
if __name__ == "__main__":
app = QApplication()
w = QMainWindow()
w.setCentralWidget(Dropdown())
w.show()
app.exec()
小部件按照收到的顺序呈现。
尝试更换
在
更改您的导入并尝试: