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.
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.
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.
Tkinter Combobox widget is in the ttk module of Tkinter, therefore, you need to import the ttk module to use this widget.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With