Mark Bell
Mark Bell

Reputation: 929

Can't reenable menus in Python tkinter on Mac

I have a GUI that I created in Tkinter which has a menu. I want a button that when clicked toggles the menu being enabled / disabled.

So I wrote the following minimal example (based off of https://mail.python.org/pipermail/tkinter-discuss/2004-September/000204.html) which works fine on my Windows 7 and Ubuntu 14.04 machines (using Python 2.7.6 and Tkinter Revision 81008). However when I tried the same code (again under Python 2.7.6 and Tkinter Revision 81008) on Mac 10.9 the menu would disable but would not reenable. Additionally, the example below also prints out what state it thinks the menu has (using entrycget) and it would print out that it thought that the menu was alternating between enabled and disabled.

Can anyone reproduce this behaviour? Is this a known bug? Or is there an alternate way to enable / disable menus on Mac

from Tkinter import *

root=Tk()

def hello():
    print "hello !"

menubar = Menu(root)
submenu = Menu(menubar, tearoff=0)
submenu.add_command(label="Hello", command=hello)
menubar.add_cascade(label='test', menu=submenu)
root.config(menu=menubar)

def toggle():
    print('I think the menu bar is %s' % menubar.entrycget(0,"state"))
    if menubar.entrycget('test', "state")=="normal":
        print('disabling')
        menubar.entryconfig('test', state=DISABLED)
        print('disbled')
    else:
        print('enabling')
        menubar.entryconfig('test', state=NORMAL)
        print('done')


b = Button(root, text='Toggle', command=toggle)
b.pack()

root.mainloop()

Upvotes: 1

Views: 979

Answers (1)

Ned Deily
Ned Deily

Reputation: 85045

As noted in here, this appears to be a bug in the Apple-supplied Tk 8.5. The Cocoa versions of Tk that Apple has been shipping since OS X 10.6 have had numerous problems many of which have been fixed in more recent versions of Tk 8.5. With the current ActiveTcl 8.5.15, your test appears to work correctly. Unfortunately, you can't easily change the version of Tcl/Tk that the Apple-supplied system Pythons use. One option is to install the current Python 2.7.7 from the python.org binary installer along with ActiveTcl 8.5.15. There is more information here:

https://www.python.org/download/mac/tcltk/

https://www.python.org/downloads/

Upvotes: 4

Related Questions