Python Forum
access subitem in tkinter Menu - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: access subitem in tkinter Menu (/thread-25586.html)



access subitem in tkinter Menu - Phraya - Apr-04-2020

I like to access a subitem e.g. 'Save' in my tkinter Menu (see code below) to activate or deactivate it.
I can access and deactivate all "first level" items like 'Main', 'File' or 'Test' - therefore I found some examples already e.g.:

self.menu.entryconfig("File", state="normal")
but I still can not reach the "second level" items e.g. 'Save'

I searched a lot already, but can't find a working solution anywhere. The only one I found is this, but it gives me an type error all time:

self.menu[1].entryconfigure('Save', state='disabled')
self.menu[1].entryconfigure('Save', state=DISABLED)
self.menu[1].entryconfigure(1)
Error:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1643, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str

Thank you for any help on this!

  def createMenu(self):
        self.menu = Menu(self.tk)
        self.tk.config(menu=self.menu)

        Main = Menu(self.menu)
        Main.add_command(label="StartScreen", command=self.menuStartScreen)
        Main.add_separator()
        Main.add_command(label="Settings", command=self.menuSettings)
        Main.add_separator()
        Main.add_command(label="Exit", command=self.client_exit)
        self.menu.add_cascade(label="Main", menu=Main)

        File = Menu(self.menu)
        File.add_command(label="Open", command=self.menuOpenFile)
        File.add_command(label="Save", command=self.menuSaveFile)
        File.add_separator()
        File.add_command(label="Print", command=self.menuPrint)
        self.menu.add_cascade(label="File", menu=File)

        Test = Menu(self.menu)
        Test.add_command(label="Test", command=self.menuTest)
        self.menu.add_cascade(label="Test", menu=Test)



RE: access subitem in tkinter Menu - deanhystad - Apr-04-2020

Why not keep a handle to the menus?
from tkinter import *

class MyProgram:
    def __init__(self):
        self.tk = Tk()
        
        self.menu = Menu(self.tk)
        self.tk.config(menu=self.menu)

        self.main_menu = Menu(self.menu, tearoff=0)
        self.main_menu .add_command(label="StartScreen")
        self.main_menu .add_command(label="Settings")
        self.main_menu .add_command(label="Exit")
        self.menu.add_cascade(label="Main", menu=self.main_menu )

        self.file_menu = Menu(self.menu, tearoff=0)
        self.file_menu.add_command(label="Open")
        self.file_menu.add_command(label="Save")
        self.file_menu.add_command(label="Print")
        self.menu.add_cascade(label="File", menu=self.file_menu)

        self.test_menu = Menu(self.menu, tearoff=0)
        self.test_menu.add_command(label="Test")
        self.menu.add_cascade(label="Test", menu=self.test_menu)

demo = MyProgram()
demo.file_menu.entryconfigure("Save", state=DISABLED)

mainloop()



RE: access subitem in tkinter Menu - Phraya - Apr-04-2020

Great idea!
Thanks a lot!!! - works perfect this way...