Python Forum
access subitem in tkinter Menu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
access subitem in tkinter Menu
#1
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)
Reply
#2
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()
Reply
#3
Great idea!
Thanks a lot!!! - works perfect this way...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter menu font size -method to change tonycat 2 632 Oct-11-2020, 02:43 AM
Last Post: tonycat
  Nesting menu buttons tkinter for python Mocap 1 879 Jul-18-2019, 11:46 AM
Last Post: metulburr
  Tkinter Gui Menu Addition Error whisperquiet 2 961 Jan-04-2019, 09:29 PM
Last Post: whisperquiet
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 2,167 Jun-15-2018, 10:42 AM
Last Post: thatguy14
  printing option menu variable in label in Tkinter SmokerX 1 3,794 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020