问题描述
有没有办法为简单的 pyqt5 应用程序更改 MacOS 上本机菜单的默认菜单标题?
我正在尝试更改上图中的“Python”菜单项。有没有办法重命名它?我可以以某种方式隐藏它吗?
menubar = self.menuBar()
for item in menubar.actions():
print(item.text())
menubar.setNativeMenuBar(False)
也无济于事(只需将“文件”移动到主窗口中即可)。
更新示例应用代码:
from PyQt5 import QtCore,QtWidgets,uic
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui,self).__init__()
# QtCore.QCoreApplication.setApplicationName('QtFoo') # doesn't work
uic.loadUi('App.ui',self)
self.show()
# app = QtWidgets.QApplication(sys.argv)
app = QtWidgets.QApplication(["MyCoolApp"])
# app.setApplicationName("QtFoo") # doesn't work
# app.setApplicationdisplayName("display Name")
window = Ui()
app.exec()
解决方法
系统菜单栏中的“python”出现是因为您从python运行脚本,一旦您打包应用程序,标题就会消失。例如下面的代码
# FileName PyQt5MenuProblem.py
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
class AppTest(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("My application")
self._createMenuBar()
def _createMenuBar(self):
self.menuBar = self.menuBar()
self.menuBar.setNativeMenuBar(False)
fileMenu = self.menuBar.addMenu("&File")
editMenu = self.menuBar.addMenu("&Edit")
if __name__== "__main__":
app = QApplication(sys.argv)
plotWindow = AppTest()
plotWindow.showMaximized()
sys.exit(app.exec_())
打包后
pyinstaller --onefile PyQt5MenuProblem.py
关键词:MacOS,用户界面,LSUIElement,pyinstaller,pyqt5
,如果您不愿意打包您的应用程序,您可以创建一个指向 python 的临时符号链接 described here。此链接可用于在标题栏中显示自定义名称的同时执行 python 应用程序:
-
转到您的应用(例如 my_app.py)文件在终端中的位置
-
创建符号链接(替换python解释器的位置,用所需名称替换“BeautifulNaming”)
ln -s /Users/Christian/miniconda3/bin/python BeautifulNaming
-
使用脚本调用 python 链接
./BeautifulNaming my_app.py