Skip to content

Instantly share code, notes, and snippets.

@andy23512
Created October 26, 2017 10:42
Show Gist options
  • Select an option

  • Save andy23512/ab214c86121e992f44c6767593edd646 to your computer and use it in GitHub Desktop.

Select an option

Save andy23512/ab214c86121e992f44c6767593edd646 to your computer and use it in GitHub Desktop.
Get menu label when click on a command in Tkinter Menu
from tkinter import *
from tkinter import ttk
def test(sheet_name, material):
print(sheet_name)
print(material)
class material_menu():
def __init__(self, name):
menubutton = ttk.Menubutton(text=name)
menubutton.grid(column=0, row=0, sticky=(N, S, E, W))
menubutton.menu = Menu(menubutton)
menubutton["menu"] = menubutton.menu
dict = {}
for sheet_name in ["Menu A", "Menu B", "MenuC"]:
dict[sheet_name] = Menu(menubutton.menu)
menubutton.menu.add_cascade(label=sheet_name, menu=dict[sheet_name])
for material in ["item1", "item2", "item3"]:
dict[sheet_name].add_command(label=material, command=lambda sheet_name=sheet_name, material=material: test(sheet_name, material))
if __name__ == "__main__":
win = Tk()
material_menu("Menubutton")
win.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment