I have a list of names in a cascading menu bar and when you click a name, I want the associated ID to be passed to a function. For example if you click 'Test1' it passes the id 4556767 to a function.
I can't figure out how to do it. I have tried using enumerate to pass an integer to a lambda function as the 'command' argument and then setting up a list of ids that corresponds to the indexes of the menu items. This however didn't work as the enumerate variable of course changes with each iteration so in the end every item returns the same value.
Is there a way to make that variable stay permanently and not change as if it was hard coded in and not a variable?
Here is my current code:
blogs = database_handler.get_blogs()
self.blog_menu_ids = []
for i, blog in enumerate(blogs):
self.blog_menu_ids.append(blog[0])
self.menu_blogs.add_command(label=blog[1],
command=lambda: self.load_blog(i))
Although if I could do that, I could just pass the id right away like so:
blogs = database_handler.get_blogs()
for blog in blogs:
self.menu_blogs.add_command(label=blog[1],
command=lambda: self.load_blog(blog[0]))