PyQt如何创建折叠菜单

编程入门 行业动态 更新时间:2024-10-23 19:22:56

PyQt如何创建折叠<a href=https://www.elefans.com/category/jswz/34/1769013.html style=菜单"/>

PyQt如何创建折叠菜单

使用PyQt、PySide2 创建折叠菜单

pytqt5

from PyQt5 import QtCore, QtGui, QtWidgetsclass CollapsibleBox(QtWidgets.QWidget):def __init__(self, title="", parent=None):super(CollapsibleBox, self).__init__(parent)self.toggle_button = QtWidgets.QToolButton(text=title, checkable=True, checked=False)self.toggle_button.setStyleSheet("QToolButton { border: none; }")self.toggle_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)self.toggle_button.setArrowType(QtCore.Qt.RightArrow)self.toggle_button.pressed.connect(self.on_pressed)self.toggle_animation = QtCore.QParallelAnimationGroup(self)self.content_area = QtWidgets.QScrollArea(maximumHeight=0, minimumHeight=0)self.content_area.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)self.content_area.setFrameShape(QtWidgets.QFrame.NoFrame)lay = QtWidgets.QVBoxLayout(self)lay.setSpacing(0)lay.setContentsMargins(0, 0, 0, 0)lay.addWidget(self.toggle_button)lay.addWidget(self.content_area)self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self, b"minimumHeight"))self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self, b"maximumHeight"))self.toggle_animation.addAnimation(QtCore.QPropertyAnimation(self.content_area, b"maximumHeight"))@QtCore.pyqtSlot()def on_pressed(self):checked = self.toggle_button.isChecked()self.toggle_button.setArrowType(QtCore.Qt.DownArrow if not checked else QtCore.Qt.RightArrow)self.toggle_animation.setDirection(QtCore.QAbstractAnimation.Forwardif not checkedelse QtCore.QAbstractAnimation.Backward)self.toggle_animation.start()def setContentLayout(self, layout):lay = self.content_area.layout()del layself.content_area.setLayout(layout)collapsed_height = (self.sizeHint().height() - self.content_area.maximumHeight())content_height = layout.sizeHint().height()for i in range(self.toggle_animation.animationCount()):animation = self.toggle_animation.animationAt(i)animation.setDuration(500)animation.setStartValue(collapsed_height)animation.setEndValue(collapsed_height + content_height)content_animation = self.toggle_animation.animationAt(self.toggle_animation.animationCount() - 1)content_animation.setDuration(500)content_animation.setStartValue(0)content_animation.setEndValue(content_height)if __name__ == "__main__":import sysimport randomapp = QtWidgets.QApplication(sys.argv)w = QtWidgets.QMainWindow()w.setCentralWidget(QtWidgets.QWidget())dock = QtWidgets.QDockWidget("Collapsible Demo")w.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dock)scroll = QtWidgets.QScrollArea()dock.setWidget(scroll)content = QtWidgets.QWidget()scroll.setWidget(content)scroll.setWidgetResizable(True)vlay = QtWidgets.QVBoxLayout(content)for i in range(10):box = CollapsibleBox("Collapsible Box Header-{}".format(i))vlay.addWidget(box)lay = QtWidgets.QVBoxLayout()for j in range(8):label = QtWidgets.QLabel("{}".format(j))color = QtGui.QColor(*[random.randint(0, 255) for _ in range(3)])label.setStyleSheet("background-color: {}; color : white;".format(color.name()))label.setAlignment(QtCore.Qt.AlignCenter)lay.addWidget(label)box.setContentLayout(lay)vlay.addStretch()w.resize(640, 480)w.show()sys.exit(app.exec_())

原文地址

更多推荐

PyQt如何创建折叠菜单

本文发布于:2024-03-09 13:49:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1725147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜单   PyQt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!