python - 在 PyQt5 中以美观的方式对按钮进行分组

标签 python pyqt pyqt5 qgroupbox

我想做这样的事情:

enter image description here

我无法找到任何方法来对这样的按钮进行分组。即,将它们包围在矩形中,并将“标题”放置到“按钮部分”。

在 PyQt5 中可以做到这一点吗?

最佳答案

您可以在 QToolButtonQLabel 旁边使用 QGroupBox:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

data = [{
        "title": " Y Axis Variables",
        "buttons": [{
                "text": "Add/Quit/Modify",
                "path_icon": "Add.png"
            },
            {
                "text": "Calculate Var",
                "path_icon": "calculate.png"
            },
            {
                "text": "Delete Cal Var",
                "path_icon": "delete.png"
            }
        ]
    },
    {
        "title": "Project",
        "buttons": [{
                "text": "Save",
                "path_icon": "save.png"
            },
            {
                "text": "Add Tab",
                "path_icon": "add.png"
            }
        ]
    }
]


class ToolButton(QtWidgets.QWidget):
    def __init__(self, text, path_icon, parent=None):
        super(ToolButton, self).__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        toolButton = QtWidgets.QToolButton()
        toolButton.setIcon(QtGui.QIcon(path_icon))
        toolButton.setIconSize(QtCore.QSize(64, 64))
        label = QtWidgets.QLabel(text)
        lay.addWidget(toolButton, 0, QtCore.Qt.AlignCenter)
        lay.addWidget(label, 0, QtCore.Qt.AlignCenter)
        lay.setContentsMargins(0, 0, 0, 0)


class GroupButton(QtWidgets.QGroupBox):
    def __init__(self, info, parent=None):
        super(GroupButton, self).__init__(parent)
        title = info["title"]
        self.setTitle(title)
        hlay = QtWidgets.QHBoxLayout(self)
        for info_button in info["buttons"]:
            text = info_button["text"]
            path_icon = info_button["path_icon"]
            btn = ToolButton(text, path_icon)
            hlay.addWidget(btn)
        hlay.setContentsMargins(5, 5, 5, 5)
        self.setFixedSize(self.sizeHint())

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        vlay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()
        for val in data:
            gb = GroupButton(val)
            hlay.addWidget(gb)
        hlay.addStretch()
        vlay.addLayout(hlay)
        vlay.addStretch()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

enter image description here

关于python - 在 PyQt5 中以美观的方式对按钮进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829622/

相关文章:

python - 使用列中的部分字符串来计算和填充 Pandas 数据框中的另一列

python - 在嵌套列表中查找匹配项

python - PyQt5 : force a specific language translation

python - PyQt5:使用设计器创建多个 View 并将它们连接到一个应用程序中

python-3.x - 在 QTableView 中正确使用 QComboBox - 设置数据和清除 QComboBox 的问题

python - 循环更新和打开文件名

java - "Importing test library ' Selenium2Library ' failed: ImportError: No module named decorator"RobotFramework maven插件

python - Qt 树形部件 python;迭代列

python - 在 Python 中将 OpenCV 3 (iplImage) 转换为 PyQt5 QImage/QPixmap

python - pyqt5计时器事件在qthread中不起作用?