我正在使用Python 3.4 / 5和Tkinter一起做一个小项目。我有一个列表框,显示目录中的所有项目。我似乎找不到一种选择项目然后将名称保存在变量中以备后用的方法。
listnotes = Listbox(notebox, selectmode=SINGLE)
listnotes.pack(side=LEFT, fill=Y, padx=10, pady=10)
listnotes.insert(END, "notes")
for i in glob.glob("\TESTFOLDER\*.txt"):
i = i[12:]
listnotes.insert(END, i)
我已经读过有关使用listnotes.get(ACTIVE)可能起作用的信息。另外,我一直没有使用python这么长时间,所以任何见识将不胜感激。
您必须使用Bindings来这样做,
例如:
listnotes.bind("<Return>", dosomething) #to bind to your enterkey
然后是与列表框关联的curselection方法
def dosomething(event):
"""
on enter, capture the value from the Listbox
and assign the value captured to value via the get method form the selected value
"""
try:
selection = listnotes.curselection()
value = listnotes.get(selection[0])
except Exception as select:
print("Nothing selected")
pass
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句