1

I'm trying to use the Tinker Menu widget, unsuccessfully. The very simple program I found at http://effbot.org/tkinterbook/menu.htm produces an empty window with no menu bar. This is the complete program:

from Tkinter import *

root = Tk()

def hello():
    print "hello!"

# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!", command=root.quit)

# display the menu
root.config(menu=menubar)

root.mainloop()

Any clue? I'm running OS X 10.9.5

I've done more reading on the subject. In Unix and Windows the menu appears at the top of the main window. In OS X, it should appear at the top of the screen (i.e., where people would expect it). In my case, the top of the screen only shows the Python menu.

Eduardo
  • 1,235
  • 16
  • 27
  • strange it works for me - python 2.7.8 on Ubuntu - does exactly what I would expect it to – gkusner Aug 20 '15 at 16:37
  • are you sure its not a sizing issue - i.e. the menu is hidden - did you try to resize the window? – gkusner Aug 20 '15 at 16:41
  • 1
    I did. Actually, the window opens fairly large, about 2x2". And it shouldn't matter, since my menu is supposed to be in the menu bar, not the window. – Eduardo Aug 20 '15 at 16:53
  • https://www.python.org/download/mac/tcltk/ - there seems to be version issues – gkusner Aug 20 '15 at 16:57
  • In an effort to solve this problem I downloaded and installed ActiveTcl 8.5. It created directories /Library/Frameworks/Tcl.framework/Versions/8.5 and /Library/Frameworks/Tk.framework/Versions/8.5. How should I change sys.path to ensure that I get the proper Tcl/Tk? – Eduardo Aug 20 '15 at 20:46

1 Answers1

1

This works for me not sure if this is what you are trying to do. If you want like a menu bar you make them a cascade menu. I always do Tkinter as a class.


    try:
        from Tkinter import * # for Python2
    except ImportError:
        from tkinter import * # for Python3

    root = Tk()

    class Application():
        def __init__(self,master):
            master.geometry('400x300+200+200') # Sets Screen Geometry
            master.title("Test Window")    # Windows Title
            self.menu = Menu(master,tearoff=0)
            master.config(menu=self.menu)
            self.subMenu = Menu(self.menu)
            self.menu.add_cascade(label="File", menu=self.subMenu) #Main Menu
            self.subMenu.add_command(label="Hello",command=self.Hello) #Submenus under File
            self.subMenu.add_command(label="Quit",command=master.quit)

        def Hello(self):
            print("Hello")


    Application(root)
    root.mainloop()