In this example, if a user selects any option in any of the drop downs, then clicks on another drop down, the previously selected item displayed a check mark beside it. Even though that choice was picked in a different menu.
from Tkinter import *
from ttk import *
choices = ['1st Choice', '2nd Choice', '3rd Choice', '4th Choice']
root = Tk()
for each in range(10):
OptionMenu(root, StringVar(), choices[0], *choices).pack()
root.mainloop()
This happens on both Python 2.7 and 3.5.
I've even moved the choices
list into the loop so it generates on each iteration and the problem still occurs. I'm assuming since the elements of the list are the same objects tkinter isn't differentiating between which OptionMenu they are in. It would seem I need a unique list every time through the loop.
Is there any way to limit the check mark to displaying only on the OptionMenu that the user has interacted with?
This is a bug in the ttk implementation of OptionMenu
. It isn't assigning a unique variable for the radiobuttons in each OptionMenu.
You can fix this with a little bit of code. Basically, you have to loop over every item in the menu and set the variable
attribute.
Here's an example:
def optionmenu_patch(om, var):
menu = om['menu']
last = menu.index("end")
for i in range(0, last+1):
menu.entryconfig(i, variable=var)
...
for each in range(10):
sv = StringVar()
om = OptionMenu(root, sv, choices[0], *choices)
om.pack()
optionmenu_patch(om, sv)
Bug tracker issue: http://bugs.python.org/issue25684
来源:https://stackoverflow.com/questions/33831289/ttk-optionmenu-displaying-check-mark-on-all-menus