OptionsMenu 只显示每个列表中的第一项?

OptionsMenu display only first item in each list?

这里我有一个基本的 OptionsMenu 和一个列表,其中包含用于我的菜单选项的列表。 What I need to do is display only the first item of the sub lists in the options menu but when that item is selected to pass the 2nd item in the sub list to a function.

目前我可以显示所有子列表并将所有子列表传递给一个函数。我遇到的主要问题是试图在下拉菜单中仅显示第一项 "ID 1" 或 "ID 2"。

import tkinter as tk


root = tk.Tk()

def print_data(x):
    print(x[1])

example_list = [["ID 1", "Some data to pass later"], ["ID 2", "Some other data to pass later"]]
tkvar = tk.StringVar()
tkvar.set('Select ID')

sellection_menu = tk.OptionMenu(root, tkvar, *example_list, command=print_data)
sellection_menu.config(width=10, anchor='w')
sellection_menu.pack()

root.mainloop()

这是我得到的:

我想要的:

如您在第二张图片中所见,菜单中仅显示 "ID 1",并且该 ID 的数据打印到控制台。

我找不到任何文档或post解决这个问题,所以它可能是不可能的。

我唯一能想到的就是这个

import tkinter as tk


root = tk.Tk()

执行一个 for 循环,获取第一个索引,即 ID

sellection_menu = tk.OptionMenu(root, tkvar,
                            *[ID[0] for ID in example_list],
                            command=print_data)

搜索给定的索引,但如果您使用大量数据,这会很慢而且不太好

def print_data(x):
    for l in example_list:
        if x == l[0]:
            print(l[1])
            break


example_list = [["ID 1", "Some data to pass later"], 
                ["ID 2", "Some other data to pass later"]]`