Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the "About Tkinter" menu in Python on OSX

Edit: I am referring to the OSX application menu, which contains the About and Preference menu items (along with others).

Perhaps this will be another easy one for someone who knows the right search terms, but after spending hours tracing code in IDLE and searching the net, I haven't quite been able to connect the dots.

I'm trying to replace the standard About menu in Python. IDLE does this at least part-way; the menu is still named "About Python" but it displays the IDLE About window. When run from Wing IDE (under X11) idle doesn't display its About window, and for some reason IDLE doesn't want to debug idle.py...

I've been able to replace "About Python" with "About MyProgramName", but I either get the usual "tk About" window, or I get no About window at all. IDLE defines a virtual event to pass control to its About window, and I'm stuck on how to define a virtual event that connects to the menu selection.

So, I have root.bind('<<about-myprogram>>', about_dialog), but how do I connect it up? tk.add_event() needs a sequence...

Any suggestions?

like image 757
LMO Avatar asked May 04 '12 00:05

LMO


2 Answers

If you're talking about constructing a menu bar with a Help entry on the menu and having an About entry on the Help menu, that's pretty basic stuff and there are good examples of that around.

  • http://effbot.org/tkinterbook/menu.htm
  • http://www.tkdocs.com/tutorial/menus.html

Either of those will explain clearly how to create top level menus for your app. If you're talking about something else, then please clarify.

I did a search for ::tk::mac::ShowPreference in the sources under my C:\Python27 dir and and ran across code in the file C:\Python27\Lib\idlelib\macosxSupport.py which looks like it's doing what you want to do (or at least close enough that you can adapt it).

def config_dialog(event=None):
    from idlelib import configDialog
    root.instance_dict = flist.inversedict
    configDialog.ConfigDialog(root, 'Settings')

root.createcommand('::tk::mac::ShowPreferences', config_dialog)

I couldn't dig up any good docs on the createcommand() method, but I did confirm that it exists on the root widget I created from root = Tk(). While looking for more info I also ran across this little discussion on the subject.

like image 103
John Gaines Jr. Avatar answered Oct 24 '22 22:10

John Gaines Jr.


I was looking for a complete example on how to make the About and Preferences menu items, but didn't find any, so I made my own. This was tested on Mac OS 10.4.11 and Mac OS 10.6.8.

from Tkinter import *
from tkMessageBox import *

def do_about_dialog():
    tk_version = window.tk.call('info', 'patchlevel')
    showinfo(message= app_name + "\nThe answer to all your problems.\n\nTK version: " + tk_version)

def do_preferences():
    showinfo(message="Preferences window")

def do_button():
    print("You pushed my button")

def main():
    global app_name
    app_name = "Chocolate Rain"
    global window
    window = Tk()
    window.title("Main")

    # find out which version of Tk we are using
    tk_version = window.tk.call('info', 'patchlevel')
    tk_version = tk_version.replace('.', '')
    tk_version = tk_version[0:2]
    tk_version = int(tk_version)

    menubar = Menu(window)
    app_menu = Menu(menubar, name='apple')
    menubar.add_cascade(menu=app_menu)

    app_menu.add_command(label='About ' + app_name, command=do_about_dialog)
    app_menu.add_separator()

    if tk_version < 85:
       app_menu.add_command(label="Preferences...", command=do_preferences)
    else:
        # Tk 8.5 and up provides the Preferences menu item
        window.createcommand('tk::mac::ShowPreferences', do_preferences)

    window.config(menu=menubar) # sets the window to use this menubar

    my_button = Button(window, text="Push", command=do_button)
    my_button.grid(row=0, column=0, padx=50, pady=30)

    mainloop()

if __name__ == "__main__":
    main()
like image 29
user1766438 Avatar answered Oct 24 '22 22:10

user1766438