Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

I have a Combobox that I can currently type into. I want it to be so that the user can only choose a valid option from the drop down.

I can't seem to find a similar question online, and I don't see anything in the documentation that could help me out.

like image 754
tristan957 Avatar asked Jul 06 '17 21:07

tristan957


People also ask

How do you disable a Combobox in Python?

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.

How do I use TTK Combobox?

Use ttk. Combobox(root, textvariable) to create a combobox. Set the state property to readonly to prevent users from entering custom values. A combobox widget emits the '<<ComboboxSelected>>' event when the selected value changes.

Where do I find the Combobox widget in tkinter?

Tkinter Combobox widget is in the ttk module of Tkinter, therefore, you need to import the ttk module to use this widget.


1 Answers

You can set the state to "readonly"

cb = ttk.Combobox(root, state="readonly", 
                  values=("one", "two", "three"))

From the python 3.6 documentation:

state: One of “normal”, “readonly”, or “disabled”. In the “readonly” state, the value may not be edited directly, and the user can only selection of the values from the dropdown list. In the “normal” state, the text field is directly editable. In the “disabled” state, no interaction is possible.

like image 138
Bryan Oakley Avatar answered Oct 03 '22 07:10

Bryan Oakley