首页 > 解决方案 > 在应用程序顶部添加菜单栏

问题描述

我试图在应用程序的最顶部添加一个菜单栏,而不是它通常想要添加它的“内部”。

快速示例:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QMenu

class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("< Menu Go Here")
        self.resize(400, 200)
        self.centralWidget = QLabel("Hello, World")
        self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.setCentralWidget(self.centralWidget)
        self._createMenuBar()

    def _createMenuBar(self):
        menuBar = self.menuBar()
        menuBar.addMenu(QMenu("&File", self))
        menuBar.addMenu("&Edit")
        menuBar.addMenu("&Help")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

会创建类似的东西:

在此处输入图像描述

但是,我希望菜单成为其上方灰线的一部分(与图标和窗口标题相同的行)。

标签: pythonpython-3.xpyqt5qmenubar

解决方案


推荐阅读