Discussion:
[Tkinter-discuss] How to enable/disable a menu?
Alan Gauld
2014-05-13 18:51:25 UTC
Permalink
I have an app with a tabbed notebook. I want to
only enable one of the main menus when the appropriate
tab is selected. When the tab is deselected I want
to disable the menu again.

I can't see any obvious properties in the Menu
object. I can mess around with flags in the event
handlers and change the menu font colours etc but
that's pretty horrible. Is there anything better?

Using Python 3.3; on Linux and Windows 8 if it makes
any difference.

TIA
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
Alejandro Autalan
2014-05-14 01:21:39 UTC
Permalink
Post by Alan Gauld
I have an app with a tabbed notebook. I want to
only enable one of the main menus when the appropriate
tab is selected. When the tab is deselected I want
to disable the menu again.
I can't see any obvious properties in the Menu
object. I can mess around with flags in the event
handlers and change the menu font colours etc but
that's pretty horrible. Is there anything better?
Using Python 3.3; on Linux and Windows 8 if it makes
any difference.
TIA
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss
Hello,
You can use the 'state' option of the menuitem.
Here's an example: http://linkode.org/tE01VF7sGPOXjvsWymBqi4

Regards
Alejandro A.
David
2014-05-14 03:06:10 UTC
Permalink
Post by Alejandro Autalan
Here's an example: http://linkode.org/tE01VF7sGPOXjvsWymBqi4
Quoting http://linkode.org/about:

"It's a kind of short living collaboration space, a dynamic pastebin."

When communicating on a *public mailing list*, please include all
relevant information *in the email message* where possible, to
maintain the integrity of the message, and the list archive. This
shows consideration to present-day readers who may not have access to
other protocols like http, and also to readers of the list archive in
the future, when the backers of some transient pastebin site may well
have abandoned it.

import tkinter as tk

root = tk.Tk()

menu = tk.Menu(root)

mfile = tk.Menu(menu)
mfile.add_command(label='Option 1')

medit = tk.Menu(menu)
medit.add_command(label='Option2')

menu.add_cascade(label='File', menu=mfile)
menu.add_cascade(label='Edit', menu=medit, state=tk.DISABLED)

root.configure(menu=menu)

#cb
enabled = False
def on_button_click():
global enabled
if enabled:
enabled = False
menu.entryconfigure(2, state=tk.DISABLED)
else:
enabled = True
menu.entryconfigure(2, state=tk.NORMAL)

#button
b = tk.Button(root, text='Enable', command=on_button_click)
b.grid(padx=60, pady=20)

root.mainloop()
Bryan Oakley
2014-05-14 12:48:43 UTC
Permalink
You can disable the button or menu item the menu is associated with, and that will disable the whole menu.

--bryan
Post by Alan Gauld
I have an app with a tabbed notebook. I want to
only enable one of the main menus when the appropriate
tab is selected. When the tab is deselected I want
to disable the menu again.
I can't see any obvious properties in the Menu
object. I can mess around with flags in the event
handlers and change the menu font colours etc but
that's pretty horrible. Is there anything better?
Using Python 3.3; on Linux and Windows 8 if it makes
any difference.
TIA
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss
ALAN GAULD
2014-05-14 15:59:35 UTC
Permalink
You can disable the button or menu item the menu is associated with, and that will disable the whole menu.?Thats what I assumed but which proprerty disables a menu - or any other widget for that matter??
I'm assuming its an inherited atttribute of some sort?


Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140514/dce76cb1/attachment.html>
Bryan Oakley
2014-05-14 16:25:10 UTC
Permalink
The "state" attribute.

Here's a working example:

import Tkinter as tk

root = tk.Tk()
menubar = tk.Menu(root)
root.configure(menu=menubar)

exampleMenu = tk.Menu(root)
exampleMenu.add_command(label="One")
exampleMenu.add_command(label="Two")
exampleMenu.add_command(label="Three")
menubar.add_cascade(label="Example", menu=exampleMenu)

menubar.entryconfigure("Example", state="disabled")

root.mainloop()
Post by Bryan Oakley
You can disable the button or menu item the menu is associated with, and
that will disable the whole menu.
Thats what I assumed but which proprerty disables a menu - or any other
widget for that matter?
I'm assuming its an inherited atttribute of some sort?
Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140514/26e55bb0/attachment.html>
ALAN GAULD
2014-05-14 23:01:53 UTC
Permalink
Thanks Bryan,?
I played around with that and its exactly what I needed.

Thanks to all who sent comments.
?
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos
Post by Alan Gauld
________________________________
From: Bryan Oakley <bryan.oakley at gmail.com>
To: ALAN GAULD <alan.gauld at btinternet.com>
Cc: "tkinter-discuss at python.org" <tkinter-discuss at python.org>
Sent: Wednesday, 14 May 2014, 17:25
Subject: Re: [Tkinter-discuss] How to enable/disable a menu?
The "state" attribute.
import Tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
root.configure(menu=menubar)
exampleMenu = tk.Menu(root)
exampleMenu.add_command(label="One")
exampleMenu.add_command(label="Two")
exampleMenu.add_command(label="Three")
menubar.add_cascade(label="Example", menu=exampleMenu)
menubar.entryconfigure("Example", state="disabled")
root.mainloop()
Post by ALAN GAULD
You can disable the button or menu item the menu is associated with, and that will disable the whole menu.?Thats what I assumed but which proprerty disables a menu - or any other widget for that matter??
I'm assuming its an inherited atttribute of some sort?
Alan G
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140515/7021abaf/attachment-0001.html>
Loading...