【问题标题】:ComboBox clears unrelated ListBox tkinter pythonComboBox 清除不相关的 ListBox tkinter python
【发布时间】:2014-08-14 19:39:47
【问题描述】:

我有一个用 tkinter 用 python 编写的非常简单的 GUI 程序:

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();

图形用户界面如下所示:

我点击ListBox 中的一些项目并点击按钮。 我得到的输出符合预期:

==========
untz untz untz
==========

==========
yeaaaah
untz untz untz
==========

我现在从ComboBox 中选择一个值,突然ListBox 中的选择消失了。点击按钮显示列表框中没有选择任何值:

==========
==========

我的问题是:为什么选择 ComboBox 项会清除 ListBox 的选择?它们没有任何关系,所以这个错误真的让我很困惑!

【问题讨论】:

    标签: python combobox listbox tkinter selection


    【解决方案1】:

    我终于找到了答案。我会把这个问题留在这里,以防其他人发现它。

    问题不在 ComboBox 中,而在 ListBox 中。如果我使用两个(不相关的)列表框,这将变得很清楚。使用两个 ListBox 时,选择将在焦点更改时清除。 从this question 及其接受的答案中,我发现将exportselection=0 添加到ListBox 会禁用导出选择的X 选择机制。

    来自effbot listbox关于X选择机制:selects something in one listbox, and then selects something in another, the original selection disappears.

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2016-03-10
      • 2012-02-24
      • 1970-01-01
      相关资源
      最近更新 更多