When you create an application with a GUI using Tkinter in Python, the name of your application appears as "Python" in the menu bar on OS X. How can you get it to appear as something else?
3 Answers
My answer is based on one buried in the middle of some forums. It was a bit difficult to find that solution, but I liked it because it allows you to distribute your application as a single cross platform script. There's no need to run it through py2app or anything similar, which would then leave you with an OS X specific package.
Anyways, I'm sharing my cleaned up version here to give it a bit more attention then it was getting there. You'll need to install pyobjc via pip to get the Foundation module used in the code.
from sys import platform # Check if we're on OS X, first. if platform == 'darwin': from Foundation import NSBundle bundle = NSBundle.mainBundle() if bundle: info = bundle.localizedInfoDictionary() or bundle.infoDictionary() if info and info['CFBundleName'] == 'Python': info['CFBundleName'] = <Your application name here> 6 Comments
pip and uses it to install additional frameworks at runtime. It's a one time hick-up for the user when they run your application the first time (assuming they have an internet connection). So I consider third party libraries a very small cost. But if you'd like to go without it, I'd imagine you can look at the source for NSBundle and just take what you need from it and insert it directly into your own code.pyobjc on the latest MBP15 - it took about half an hour. Not an option.pyobjc for this are the Foundation, objc, and CoreFoundation modules. I have verified that I am able to copy just those 3 folders from site-packages on one Python installation to another and that it works. If that's not light enough on requirements for you, then I believe those 3 modules are open source and you could probably cut and paste the relevant code from them into a single, slimmed down source file just for this single task.In new versions of macOS, the accepted answer apparently doesn't work anymore. I found a workaround that gets rid of the menu item named "Python", so you can replace it with a menu that has the name of your app. It works at least on macOS 14.4 with Python 3.12 and Tcl/Tk 8.6.14. I figured it out with some information from tkdocs and some trial and error.
After some basic setup,
from tkinter import * APP_NAME = "Jim's app" window = Tk() window.title(APP_NAME) menu = Menu() you can make the Python menu disappear with the following commands, using the undocumented name parameter of the Menu constructor – they must be in precisely this order:
# Get rid of the "Python" menu python_menu = Menu(menu, name='apple') menu.add_cascade(menu=python_menu) window['menu'] = menu python_menu.destroy() Then, you add your own menus. For example,
# App-name menu app_menu = Menu(menu) menu.add_cascade(menu=app_menu, label=APP_NAME) app_menu.add_command(label=f'About {APP_NAME}') app_menu.add_separator() app_menu.add_command(label=f'Settings...') app_menu.add_separator() app_menu.add_command(label=f'Quit {APP_NAME}', command=window.destroy) # File menu file_menu = Menu(menu) menu.add_cascade(menu=file_menu, label=f'File') file_menu.add_command(label=f'New...') file_menu.add_command(label=f'Open...') # Help menu help_menu = Menu(menu) menu.add_cascade(menu=help_menu, label='Help') help_menu.add_command(label=f'{APP_NAME} Help') (The Help menu even has that nice search box, like in other apps.) Of course you still have to write functions that handle the menu commands, and add them into the add_command calls as the command arguments. Finally, to get the app running,
# GUI Label(window, text=f'Just testing my new app {APP_NAME}').grid(padx=20, pady=20) window.mainloop() It's not pretty (and unfortunately the app-name on the first menu is not in bold text), but at least it works. To ensure portability you should probably run the Python-menu-destroying code only if the program is running on macOS, if sys.platform == 'darwin'.
Comments
May not be quite what you need but I am surprised no one has mentioned the simple, platform independent way (works with Python 3.x on Win 7) :
from tkinter import Tk root = Tk() root.title( "Your title here" ) # or root.wm_title and if you want to change the icon:
''' Replace the default "Tk" icon with an Application-specific icon ''' ''' (that is located in the same folder as the python source code). ''' import sys from tkinter import PhotoImage program_directory = sys.path[ 0 ] IconFile = os.path.join( program_directory ) + "\ApplicationIcon.gif" IconImage = PhotoImage( file = IconFile ) root.tk.call( 'wm', 'iconphoto', root._w, IconImage ) root.mainloop()
Tk? There are several options and I would guess thatbaseNameis what you are looking for.