如何在pyqt5中创建这样的Button?

梅斯珀

在此处输入图片说明 如何使用显示的图片创建带有列表选择器的按钮?

永乐

您可以QToolButton通过设置来使用QMenu

from PyQt5 import QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        button = QtWidgets.QToolButton(
            icon=QtGui.QIcon("plus.png"),
            popupMode=QtWidgets.QToolButton.InstantPopup
        )
        menu = QtWidgets.QMenu(button)
        menu.addAction(QtGui.QIcon("insert.png"), "Insert multiple rows")
        menu.addSeparator()
        menu.addAction("Place new rows above selected row")
        menu.addAction("Place new rows bellow selected row")
        menu.addAction("Place new rows at the end of the data view")
        button.setMenu(menu)
        toolbar = self.addToolBar("tools")
        toolbar.addWidget(button)
        tableWidget = QtWidgets.QTableWidget(10, 10)
        self.setCentralWidget(tableWidget)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章