首页 > 解决方案 > 一些工具栏没有显示,也不能移动

问题描述

我正在尝试在 PyQt5 上构建一个 GUI。最近我试图添加一个工具栏。但是当我使用 MAC OS 时,保留的项目没有显示出来。工具栏也是静态的,我无法真正移动它们。

我尝试使用 stackoverflow Missing menuBar in PyQt5中提供的解决方案

    from PyQt5 import QtGui
    from PyQt5.QtWidgets import QApplication, QMainWindow,QAction
    from PyQt5.QtGui import QIcon
    import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.title = "PyQt5 Tool Bars"
        self.top = 100
        self.left = 100
        self.width = 680
        self.height = 500

        self.InitWindow()

    def InitWindow(self):
        #exitAct = QAction(QIcon('exit.png'), ' GoOut', self)
        #exitAct.setShortcut('Ctrl+Q')
        #exitAct.triggered.connect(self.CloseApp)

        exitAct = QAction(QIcon('exit.png'), '&Exit', self)
        print(exitAct.menuRole())  # It prints "1".
        QAction::TextHeuristicRole
        exitAct.setMenuRole(QAction.NoRole)

        copyAct = QAction(QIcon('copy.png'), 'Copy', self)
        copyAct.setShortcut('Ctrl+C')

        pasteAct = QAction(QIcon('paste.png'), 'Paste', self)
        pasteAct.setShortcut('Ctrl+V')

        deleteAct = QAction(QIcon('del.png'), 'Delete', self)
        deleteAct.setShortcut('Ctrl+D')

        saveAct = QAction(QIcon('save.png'), 'Save', self)
        saveAct.setShortcut('Ctrl+S')



        self.toolbar = self.addToolBar('Toolbar')

        self.toolbar.addAction(exitAct)
        self.toolbar.addAction(copyAct)
        self.toolbar.addAction(pasteAct)
        self.toolbar.addAction(deleteAct)
        self.toolbar.addAction(saveAct)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.show()


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

我希望能看到所有的图标。

这就是我所看到的

标签: pythonuser-interfaceiconspyqt5

解决方案


推荐阅读