Search code examples
pythontkintercombobox

Tkinter - Dynamically add and delete values from one combobox based on another combobox selection


I have two Ttk comboboxes, both of them with the exact same values: A, B, C, D. I need to delete from one of them the value I selected on the other one, and, if I change my selection, I want that deleted value back and delete the new value. This is to prevent from choosing the same value on both combos.
That is:
On combo1 I select A. Delete A from combo2.
Then I select B on combo1. Add A into combo2; delete B from it.

Thanks in advance!


Solution

  • Your solution can be simplified as below:

    def update_combos(self, event):
        self.cmb_group1["values"] = [x for x in self.lst_combo if x != self.cmb_group2.get()]
        self.cmb_group2["values"] = [x for x in self.lst_combo if x != self.cmb_group1.get()]