ComboBox清除无关的列表框tkinter python
我有一个非常简单的GUI程序,使用tkinter用python编写:ComboBox清除无关的列表框tkinter python,python,combobox,listbox,tkinter,selection,Python,Combobox,Listbox,Tkinter,Selection,我有一个非常简单的GUI程序,使用tkinter用python编写: from Tkinter import * from ttk import Combobox class TestFrame(Frame): def __init__(self, root, vehicles): dropdownVals = ['test', 'reallylongstring', 'etc'] listVals = ['yeaaaah', 'mhm mhm',
from Tkinter import *
from ttk import Combobox
class TestFrame(Frame):
def __init__(self, root, vehicles):
dropdownVals = ['test', 'reallylongstring', 'etc']
listVals = ['yeaaaah', 'mhm mhm', 'untz untz untz', 'test']
self.dropdown = Combobox(root, values=dropdownVals)
self.dropdown.pack()
listboxPanel = Frame(root)
self.listbox = Listbox(listboxPanel, selectmode=MULTIPLE)
self.listbox.grid(row=0, column=0)
for item in listVals:
self.listbox.insert(END, item) # Add params to list
self.scrollbar = Scrollbar(listboxPanel, orient=VERTICAL)
self.listbox.config(yscrollcommand=self.scrollbar.set) # Connect list to scrollbar
self.scrollbar.config(command=self.listbox.yview) # Connect scrollbar to list
self.scrollbar.grid(row=0, column=1, sticky=N+S)
listboxPanel.pack()
self.b = Button(root, text='Show selected list values', command=self.print_scroll_selected)
self.b.pack()
root.update()
def print_scroll_selected(self):
listboxSel = map(int, self.listbox.curselection()) # Get selections in listbox
print '=========='
for sel in listboxSel:
print self.listbox.get(sel)
print '=========='
print ''
# Create GUI
root = Tk()
win = TestFrame(root, None)
root.mainloop();
GUI如下所示:
我在列表框中点击几个项目
,然后点击按钮。
我得到的输出与预期一致:
==========
untz untz untz
==========
==========
yeaaaah
untz untz untz
==========
我现在从组合框
中选择一个值,列表框
中的选择突然消失。点击按钮表示列表框中未选择任何值:
==========
==========
我的问题是:为什么选择组合框项目会清除列表框的选择?他们没有任何关系,所以这个错误真的让我困惑 我终于找到了答案。我将把这个问题留在这里,以防其他人发现它 问题不在组合框中,而是在列表框中。如果我使用两个(不相关的)列表框,这一点就很清楚了。使用两个列表框,当焦点改变时,选择将被清除。 从中,我发现将
exportselection=0
添加到列表框中会禁用导出选择的X选择机制
从大约X选择机制:在一个列表框中选择某个内容,然后在另一个列表框中选择某个内容,原始选择将消失。