17

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?

5
  • 2
    Related stackoverflow.com/questions/8695926/… Commented Jun 7, 2015 at 10:53
  • Sigh. You'd think tkinter would just do this automatically when you set the title. Commented May 13, 2021 at 1:22
  • @EdwardFalk - On a Mac, windows and apps are not generally synonymous the way they are on Windows and most Linux GUIs. So... no, I wouldn't expect that behavior. I'd expect some easy way of achieving the desired result, though. I'm curious why I posted this Q&A at 3:30 AM my timezone - what was I doing this night 6 years ago? It was just 3 weeks before my wedding. Commented May 14, 2021 at 3:57
  • Relevant XKCD: xkcd.com/979 Commented May 14, 2021 at 17:06
  • It might be a little late, but still would be interesting. Have you tried to alter the keyword arguments of Tk ? There are several options and I would guess that baseName is what you are looking for. Commented Dec 1, 2022 at 21:49

3 Answers 3

17
+200

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> 
Sign up to request clarification or add additional context in comments.

6 Comments

But this requires another third party library. Is there no way to do it within Tk?
@Zizouz212: I have a script which automatically installs 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.
Tried to install pyobjc on the latest MBP15 - it took about half an hour. Not an option.
Well, that was mostly my fault. Apparently, PyObjC comes with OSX preinstalled. Previously I thought that I have installed it myself missing dedicated virtualenv and therefore uninstalled it. Installing it again takes A LOT of time, mostly due to utilizing just 1 core, see stackoverflow.com/questions/26228136/…
@Andrei - All you actually need from 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.
|
1

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

-4

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

6 Comments

I asked specifically about OS X (and want an answer that works on 2.7, although that probably doesn't make a difference.) Have you actually tried this on OS X and seen that it works? I have to assume I tried this before asking a year ago... I don't actually have a computer running OS X in front of me to test with right now. I'll look into this later when I have a chance to test on a Mac...
try this code on OSX with Pyton 2.x from Tkinter import * root = Tk() root.title( "Your title here" ) w = Label(root, text="Hello, world!") w.pack() root.mainloop()
Reposted due to stupid 5 minute limit on editing comments. For python 2.x use "from Tkinter" instead of "from tkinter" (for 3.x). Tkinter/tkinter is designed to be cross-platform compatable on at least Linux, Win and Mac. Try this code on OS X with Python 2.x - it's slightly modified from link: root = Tk() root.title( "Your title here" ) w = Label(root, text="Hello, world!") w.pack() root.mainloop()
The question was regarding how to get rid of the "Python" program name in the top left of a Menu when running on MacOS. You answered a completely different question (setting the title in the middle of the app frame) on a different platform (Windows).
@user1459519 - sure, sure, it should work on macOS (Monterey, Python 3.8) but the fact is it doesn’t.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.