我有一个带有关联“选择”按钮的列表框。我希望我的 GUI 双击任何列表框值都会调用此按钮的命令。 My attempt (below) works when an option is selected and the user double-clicks ANYWHERE in the window.我希望它仅在双击选择本身(蓝色突出显示的行)时起作用。
执行此操作的最佳方法是什么?
from tkinter import *
def func1():
print("in func1")
def func2():
print("in func2")
def selection():
try:
dictionary[listbox.selection_get()]()
except:
pass
root = Tk()
frame = Frame(root)
frame.pack()
dictionary = {"1":func1, "2":func2}
items = StringVar(value=tuple(sorted(dictionary.keys())))
listbox = Listbox(frame, listvariable=items, width=15, height=5)
listbox.grid(column=0, row=2, rowspan=6, sticky=("n", "w", "e", "s"))
listbox.focus()
selectButton = Button(frame, text='Select', underline = 0, command=selection)
selectButton.grid(column=2, row=4, sticky="e", padx=50, pady=50)
root.bind('<Double-1>', lambda x: selectButton.invoke())
root.mainloop()
最佳答案
将 root.bind(...)
更改为 listbox.bind(...)
关于python - 我可以双击 tkinter 列表框选项来调用 Python 中的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25088000/