How to create an arbitrary number of identical OptionMenus in tkinter?

Viewed 31

I'm trying to create an arbitrary number of identical OptionMenus in tkinter using a for loop, however as soon as I select an option in one, all of them are updated. I would like each OptionMenu to act independently of one another while still maintaining the same set of options between each.

Here's my code:

def __initialize_dropdowns(self, length):

    default = tk.StringVar()
    default.set(" ")

    for n in range(length):
        dropdown = tk.OptionMenu(self.frame, default, *self.CHARSET)
        dropdown.grid(row=0, column=n)

Any help would be appriciated.

1 Answers

If this code could help.

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

num = tk.StringVar(root)
nums = ('', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', '10', '11', '12', '13', '14', '15',
        '16', '17', '18', '19', '20', '21', '22', '23', '24'
        )
num.set(nums[0])

optmenu = tk.OptionMenu(frame, num, *nums)
optmenu.pack(side=tk.LEFT)

tk.mainloop()

Output:

enter image description here

Related