How can I disable typing in a ttk.Combobox tkinter?

TkinterServer Side ProgrammingProgramming

The ttk.Combobox is used to create dropdown menus in the Entry Widgets. To create the options, we just simply pass the strings to the values object of combobox. We can disable the combobox by passing the state as “readonly”.

Example

In the following example, we will create a combobox whose state is disabled.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter window
win.geometry("750x250")
#Create an instance of StringVar
var= StringVar()
#Create an Label
Label(win, text="Select any Language", font= ('Helvetica 15 bold')).pack(pady=20)
#Create Object of Tkinter Combobox
combobox= ttk.Combobox(win, textvariable= var, values=["C++","Java","Python","Rust","Go","JavaScript"])
combobox.pack()
win.mainloop()

Output

Running the above code will display a window containing a combobox menu. We can select any option in the dropdown list.

Now add state = "readonly" in the Combobox object, it will make the Combobox Entry Widget disabled.

raja
Published on 15-Apr-2021 13:32:33
Advertisements