简体   繁体   English

取消选择组合框中的选定选项

[英]Unselecting selected option in combobox

I wrote a function which implements something like when user selects 1st option in Combobox this option shouldn't be available in rest comboboxes.我写了一个函数,它实现了类似当用户在 Combobox 中选择第一个选项时此选项在其余组合框中不可用的功能。 It's working but when I choose all options after another no option is available to select.它正在工作,但是当我一个接一个地选择所有选项时,没有可供选择的选项。

I want to make it working like when user selects for example 1st option in combobox, this option isn't available in rest comboboxes but when user unselects this 1st option, this option is available again everywhere.我想让它像用户在组合框中选择第一个选项时那样工作,这个选项在其余组合框中不可用,但是当用户取消选择这个第一个选项时,这个选项在任何地方都可用。

Here's the code which I tried to implement and I have no idea how can I repair this code.这是我尝试实现的代码,但我不知道如何修复此代码。

toChoose = [
    "Option 1",
    "Option 2",
    "Option 3",
    "Option 4",
    "Option 5",
    "Option 6",
    "Option 7"
]

box = Combobox(self, values=toChoose, state="readonly")
box2 = Combobox(self, values=toChoose, state="readonly")
box3 = Combobox(self, values=toChoose, state="readonly")
box4 =  Combobox(self, values=toChoose, state="readonly")

selected = set()


def on_select(event):
    value = event.widget.get()

    if value in selected:
        return

    selected.add(value)

    values = [val for val in toChoose if val not in selected]
    box.configure(values=values)
    box2.configure(values=values)
    box3.configure(values=values)
    box4.configure(values=values)

    box['values'] = [x for x in toChoose if x not in selected]
    box2['values'] = [x for x in toChoose if x not in selected]
    box3['values'] = [x for x in toChoose if x not in selected]
    box4['values'] = [x for x in toChoose if x not in selected]


box.bind("<<ComboboxSelected>>", on_select)
box2.bind("<<ComboboxSelected>>", on_select)
box3.bind("<<ComboboxSelected>>", on_select)
box4.bind("<<ComboboxSelected>>", on_select)

You need to get all the selected values in those comboboxes, then the available values of a combobox are its selected value (if any) and values that are not in the selected values:您需要获取这些组合框中的所有选定值,然后组合框的可用值是其选定值(如果有)和不在选定值中的值:

def on_select(event):
    combolist = (box, box2, box3, box4)
    # get all the selected values
    selected = [cb.get() for cb in combolist if cb.get()]
    # go through each combobox
    for cb in combolist:
        # available values = (self selected value) + (values not in selected)
        cb['values'] = [v for v in toChoose if cb.get() == v or v not in selected]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM