Python Tkinter 多选列表框 [英] Python Tkinter multiple selection Listbox
问题描述
我在这里尝试了搜索,但没有找到正确答案.
我有一个使用 selection='multiple'
设置的列表框.然后我尝试通过代码 name.get(ACTIVE)
获取用户选择的所有选项的列表.问题是它并不总是获得我在列表框 GUI 中突出显示的所有选项.
I tried a search on here but not come across the correct answer.
I have a listbox which is setup with selection='multiple'
.
I then try to obtain a list of all the options the user has selected, by the code name.get(ACTIVE)
.
the problem is that it doesn't not always get all the options I have highlighted in the listbox GUI.
如果我突出显示一个,它会正确地将其带回来.
如果我突出显示两个或更多(通过单击每个)它只会返回我选择的最后一个项目
如果我有多个突出显示,然后单击取消突出显示一个,则是我单击的最后一个即使未突出显示也会返回.
If I highlight one, it brings this back correctly.
If I highlight two or more (by a single click on each) it only returns the last item I selected
If I have multiple highlighted, but then click to un-highlight one, it's this last one I clicked that gets returned even though it unhighlighted.
任何帮助都会很棒.谢谢.我期待代码能够恢复突出显示的内容.
Any help would be really great. thanks. I'm expecting for the code to bring back what ever is highlighted.
设置列表框的代码是:
self.rightBT3 = Listbox(Frame1,selectmode='multiple',exportselection=0)
检索选择的代码是:
selection = self.rightBT3.get(ACTIVE)
这是应用程序运行时的屏幕截图,在顶部您可以看到控制台只注册了一个选择(我点击了最后一个).
This is a screenshot of what the Application looks like in action, at the top you can see the console only registered the one selection (last one I clicked).
推荐答案
在 Tkinter 列表框中获取所选项目列表的正确方法似乎是使用 self.rightBT3.curselection()
,它返回一个包含所选行的从零开始的索引的元组.然后您可以使用这些索引 get()
每一行.
It seems the correct way to get a list of selected items in a Tkinter listbox is to use self.rightBT3.curselection()
, which returns a tuple containing the zero-based index of the selected lines. You can then get()
each line using those indices.
(不过我还没有实际测试过)
(I haven't actually tested this though)
这篇关于Python Tkinter 多选列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!