英文:
How do you get all the contents from a tkinter "multiple select" Listbox?
问题
你可以在这里看到,我正在创建一个tkinter的ListBox
并向其中填充3个项目。我只需要能够记录用户在按下按钮时选择了什么!
我知道当你点击按钮时,**bigListbox.curselection()
**可以正确获取项目的索引并将它们插入到show
标签中。
但是我需要实际收集当前选定的字符串,并在点击"Send"按钮时保存到文件中。
def showSelected():
show.config(text=bigListbox.curselection())
bigListbox = Listbox(rootWindow, selectmode="multiple")
bigListbox.pack()
# 从文件加载工作站
bigArr=["MYHSS", "MY", "MORE", "Aloysius"]
myIndex = len(bigArr)
for index in range(0, myIndex):
bigListbox.insert(myIndex, bigArr[index])
Button(rootWindow, text='Send', command=showSelected).pack(pady=20)
show = Label(rootWindow)
show.pack()
英文:
You can see here that I am creating a tkinter ListBox
and populating it with 3 items. All I need to do is be able to record what the user has selected when they press the button!
I know that when you click the Button, bigListbox.curselection()
gets the indexes of the items correctly and inserts them into the show
label.
But I need to actually collect the currently selected strings and save them to a file upon clicking send.
def showSelected():
show.config(text=bigListbox.curselection())
bigListbox = Listbox(rootWindow, selectmode="multiple")
bigListbox.pack()
# Load workstations from file
bigArr=["MYHSS", "MY", "MORE", "Aloysius"]
myIndex = len(bigArr)
for index in range(0, myIndex):
bigListbox.insert(myIndex, bigArr[index])
Button(rootWindow, text='Send', command=showSelected).pack(pady=20)
show = Label(rootWindow)
show.pack()
答案1
得分: 3
你可以使用Listbox
的.get()
方法来获取指定索引处的字符串:
def showSelected():
# 创建一个由所选项目组成的逗号分隔列表
items = ", ".join(bigListbox.get(idx) for idx in bigListbox.curselection())
show.config(text=items)
英文:
You can use .get()
of Listbox
to get the string at specified index:
def showSelected():
# create a comma separated list of selected items
items = ", ".join(bigListbox.get(idx) for idx in bigListbox.curselection())
show.config(text=items)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论