ArtOfWarfare
ArtOfWarfare

Reputation: 21517

Change title of Tkinter application in OS X Menu Bar

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?

Upvotes: 17

Views: 5605

Answers (3)

Jim Danner
Jim Danner

Reputation: 555

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'.

Upvotes: 0

user1459519
user1459519

Reputation: 720

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

Upvotes: -4

ArtOfWarfare
ArtOfWarfare

Reputation: 21517

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>

Upvotes: 16

Related Questions