How to disable a Combobox in Tkinter?

TkinterServer Side ProgrammingProgramming

The Combobox widget is similar to the OptionMenu widget in Tkinter which gives the user a choice to select from the group of options. The Combobox widget allows users to select the option with an Entry widget that adds selected menu items from the dropdown list.

We can Enable or Disable the options in the given Combobox widget by providing the state property. The state property forces to make a widget either active or disabled. To disable the Combobox widget, we have to set the state property as readonly or disabled.

Example

#Import the required Libraries
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter frame
win = Tk()

#Set the geometry of Tkinter Frame
win.geometry("750x250")

#Define a Label widget
Label(win, text= "Select an Option from the List", font=('Aerial', 14, 'bold')).pack(pady=15)

#Create a Combobox with list of items
var= StringVar()
my_combobox= ttk.Combobox(win, textvariable=var, values=["High", "Mid","Low"], state= 'disabled')
my_combobox.pack()

#Create a Button widget
win.mainloop()

In the above code snippet, we have disabled the Combobox widget by disabling the state. Now, to make the widget fully functional, just change the state of the widget using state= ‘normal’.

Output

raja
Published on 03-May-2021 09:37:14
Advertisements