pyqt5 default native menu text on MacOS

676 Views Asked by At

Is there a way to change default menu title for native menu on MacOS for a simple pyqt5 app?

enter image description here

I'm trying to change "Python" menu entry on above image. Is there a way to rename it? Can I hide it somehow?

This code prints only "File":

menubar = self.menuBar()
for item in menubar.actions():
    print(item.text())

menubar.setNativeMenuBar(False) doesn't help too (just move "File" into the MainWindow).

Update Sample app code:

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()
2

There are 2 best solutions below

0
Christian Karcher On BEST ANSWER

If you are not willing to package your app, you could create a temporary symbolic link to python as described here. This link can be used to execute python apps while displaying a custom name in the title bar:

enter image description here

  1. go to the location of your app (e.g. my_app.py) file in the terminal

  2. create symbolic link (replace location to your python interpreter, replace "BeautifulNaming" with desired name)

    ln -s /Users/Christian/miniconda3/bin/python BeautifulNaming

  3. call link to python with your script

    ./BeautifulNaming my_app.py

2
Stepan On

The "python" in the system menu bar appears because you run the script from the python, as soon as you package the application the title will disappear. For example the following code

# 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_())    

after packaging with

pyinstaller --onefile PyQt5MenuProblem.py 

looks like
enter image description here

Key words: MacOS, User Interface, LSUIElement, pyinstaller, pyqt5