pyqt5 default native menu text on MacOS
Asked Answered
P

2

5

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()
Pontiff answered 6/1, 2021 at 6:20 Comment(6)
try with: QtCore.QCoreApplication.setApplicationName("QtFoo")Amabil
@Amabil Doesn't work for me.Pontiff
Have a try with self.setWindowTitle(mytitle) in your __init__().Colchester
@Colchester Same result :-(Pontiff
@Pontiff is the application going to be packaged?Soilure
@Soilure Yes, but right now I just run it with python3 gui,py command.Pontiff
C
3

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

Coquette answered 11/1, 2021 at 14:30 Comment(0)
E
3

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

Eustasius answered 11/1, 2021 at 6:23 Comment(2)
Tip: 1) the setNativeMenuBar() is not required in the scope of the question, I'd remove it as it might lead to confusion making think that it might be a requirement; 2) using the -w switch in pyinstaller should ensure that the appropriate .app bundle is created, which will correctly create the correct plist file that will make the menu title correct (theoretically based on windowTitle() in that case, but I'm not sure).Soilure
Thank you for your answer. I planned to award this answer my bounty too but unfortunately there is no way to award more than one answer :-(Pontiff
C
3

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

Coquette answered 11/1, 2021 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.