0

The code and output can be found below. For some reason, when running this simple program, all of the menu commands change the map_select_menu_button to "forest". I have no idea why this is happening. Even though the labels on the menu commands are all correct, all of them pass "forest" as their parameter to the update_map_menu function. Any ideas?

Code

import tkinter as tk

root = tk.Tk()

map_list = ["default", "plain", "forest"]

map_select_menu_button_var = tk.StringVar()
map_select_menu_button_var.set(map_list[0])
map_select_menubutton = tk.Menubutton(root, textvariable=map_select_menu_button_var)

map_select_menu = tk.Menu(map_select_menubutton)
map_select_menubutton.configure(menu=map_select_menu)

for map in map_list:
    print(map)
    map_select_menu.add_command(label=map, command=lambda: update_map_menu(map))

map_select_menubutton.pack()

def update_map_menu(map):
    print(map)
    map_select_menu_button_var.set(map)

root.mainloop()

Output

In this output, I select all three buttons once.

default
plain
forest
forest
forest
forest
BrianZhang
  • 143
  • 8
  • @acw1668 yep, thanks so much man. I would have never been able to figure this out myself... such an unexpectable behaviour for someone who is unfamiliar with it. – BrianZhang Aug 21 '21 at 01:19

0 Answers0