Search code examples
pythontkintercombobox

ttk combobox selection synchronize the state of entry


I would like to realize a special case, when i select the value in combobox, the entry state changes correspondingly, e.g. when i select 1 in levels, the entry of level-1 is active, while the entry of level-2 is disabled, can anyone give me some suggestion. when i select 2 in levels, both of the state of entry are enabled.

# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk



class MainGui(tk.Tk):

    def __init__(self):

        super().__init__()
        self.create_widgets()

    def create_widgets(self):

        label_1 = tk.Label(self,
                  text = "Levels:",
                  anchor = 'w')
        label_1.grid(row=0, column=0)

        ComboBox1 = ttk.Combobox(self, width = 10, state = 'readonly')
        ComboBox1["values"] = ("1", "2")
        ComboBox1.current(0)
        ComboBox1.grid(row=0, column=1)

        label1 = tk.Label(self,
                  text = "Level-1:",
                  anchor = 'w')
        label1.grid(row=1, column=0)

        entry1 = tk.Entry(self,
                      width = 10,
                      )
        entry1.grid(row=1, column=1)

        label2 = tk.Label(self,
                  text = "Level-2:",
                  anchor = 'w')
        label2.grid(row=2, column=0)

        entry2 = tk.Entry(self,
                      width = 10,
                      )
        entry2.grid(row=2, column=1)


def main():

    # gui
    root = MainGui()
    root.mainloop()


if __name__ == '__main__':
    main()


Solution

  • You can bind the virtual event <<ComboboxSelected>> on ComboBox1 and update the state of Entry2 in the event callback which will be executed whenever the selection of ComboBox1 is changed. But you need to change ComboBox1 and entry2 to instance variables in order to access them inside the event callback:

    def create_widgets(self):
        ...
        # changed ComboBox1 to instance variable self.ComboBox1
        self.ComboBox1 = ttk.Combobox(self, width = 10, state = 'readonly')
        self.ComboBox1["values"] = ("1", "2")
        self.ComboBox1.current(0)
        self.ComboBox1.grid(row=0, column=1)
        self.ComboBox1.bind("<<ComboboxSelected>>", self.on_select)
        ...
        # changed entry2 to instance variable self.entry2
        self.entry2 = tk.Entry(self,
                              width = 10,
                              state = "disabled"  # initially disabled
                              )
        self.entry2.grid(row=2, column=1)
    
    def on_select(self, event=None):
        # set state of entry2 based on selected value
        self.entry2.config(state="normal" if self.ComboBox1.get()=="2" else "disabled")