日安,
我有一个python应用程序,它生成多个列表框,每个列表都有自己的数据列表。这些列表框根据用户生成的列表的长度动态创建。
我有一个按钮,当单击该按钮时,我希望触发一些代码来执行活动列表框(从列表中删除值等)。
因此,我的计划是遍历所有的列表框,只有在列表框有焦点的情况下才会深入研究。但是遗憾的是,经过2-3个小时的问题和更多的文档之后,我无法确定某件事情是否有焦点。
提前感谢!
发布于 2014-10-14 16:46:03
小部件能够发出<FocusIn>和<FocusOut>事件,因此您可以绑定回调,以便手动跟踪哪个列表框具有焦点。示例:
from Tkinter import *
class App(Tk):
def __init__(self, *args, **kargs):
Tk.__init__(self, *args, **kargs)
self.focused_box = None
for i in range(4):
box = Listbox(self)
box.pack()
box.insert(END, "box item #1")
box.bind("<FocusIn>", self.box_focused)
box.bind("<FocusOut>", self.box_unfocused)
button = Button(text="add item to list", command=self.add_clicked)
button.pack()
#called when a listbox gains focus
def box_focused(self, event):
self.focused_box = event.widget
#called when a listbox loses focus
def box_unfocused(self, event):
self.focused_box = None
#called when the user clicks the "add item to list" button
def add_clicked(self):
if not self.focused_box: return
self.focused_box.insert(END, "another item")
App().mainloop()在这里,单击按钮将添加“另一项”到任何列表框有焦点。
https://stackoverflow.com/questions/26365546
复制相似问题