我可以双击tkinter Listbox选项以在Python中调用函数吗? [英] Can I double-click a tkinter Listbox option to invoke function in Python?
本文介绍了我可以双击tkinter Listbox选项以在Python中调用函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有关联的选择"按钮的列表框.我希望我的GUI能够双击任何Listbox值来调用此按钮的命令.我的尝试(下面)在选择一个选项时工作,并且用户双击窗口中的任何位置.我希望它仅在双击选择本身(蓝色突出显示的行)时才能工作.
I have a Listbox with an associated "Select" button. I want my GUI such that a double-click on any Listbox value invokes this button's command. My attempt (below) works when an option is selected and the user double-clicks ANYWHERE in the window. I want it to work only when the selection itself (blue highlighted row) is being double-clicked.
做到这一点的最佳方法是什么?
What is the best way to do this?
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(...)
这篇关于我可以双击tkinter Listbox选项以在Python中调用函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文