I need to get selected folder number from Menu to manipulate such data next time. Tried to implement optionMenu to make program easier, but I need to create Frame, which contains several Menus.
Using callback(menu) method I tried to get the number, however program prints first two numbers.
from tkinter import *
import tkinter as tk
from tkinter import ttk
root = Tk()
root.geometry("300x150")
frame = Frame(root)
frame.pack()
menu_frame = Menu(root)
file_menu = Menu(menu_frame, tearoff=0)
folders = ['25', '70', '200']
sub_menu = Menu(menu_frame, tearoff=0)
for folder in folders:
sub_menu.add_command(label=folder, command=lambda: callback(sub_menu))
file_menu.add_cascade(label='Open folder... ', menu=sub_menu)
menu_frame.add_cascade(label='File', menu=file_menu)
file_menu.add_separator()
file_menu.add_command(label='Exit', command=root.destroy)
def callback(menu):
for index in range(2):
x = menu.entrycget(index, "label")
print(x)
CodePudding user response:
Seems like you want to add folder
as an argument to callback()
, which you can pass in via the lambda
sub_menu = Menu(menu_frame, tearoff=0)
for folder in folders:
sub_menu.add_command(label=folder, command=lambda f=folder: callback(f))
def callback(folder):
print(folder)