curselection
方法可返回列表框中所选项的列表。列表框的 bbox
方法将给出一个相对于列表框自身的项目坐标。您可以使用 winfo_rootx
和 winfo_rooty
来获取列表框在屏幕上的绝对坐标。您可以利用所有这些信息将菜单放置在所选项目附近。
例如:
def show_popup(event):
selection = event.widget.curselection()
if selection:
item = selection[0]
rootx = event.widget.winfo_rootx()
rooty = event.widget.winfo_rooty()
itemx, itemy, itemwidth, itemheight = event.widget.bbox(item)
popup_menu.tk_popup(rootx+event.widget.winfo_width()-10, rooty+itemy+10)