如何在能够输入的同时将输入限制为仅 AutocompleteCombobox 中的可用选项?
我正在使用 ttkwidgets.autocomplete
中的 AutocompleteCombobox
作为我的选择小部件。虽然所有功能都很好(例如能够在键入时过滤列表),但我希望能够键入它,但只能从可用选项中键入,即,我不应该能够键入自定义值。 我尝试使用 state=readonly 但它不允许我在组合框中输入。 任何解决方案将不胜感激。
I am using AutocompleteCombobox
from ttkwidgets.autocomplete
for my selection widget. While all the features are good (like being able to filter the list while typing), I want to be able to type in it but only from the available options ie., I should not be able to type in custom values.
I tried using state=readonly but it doesn't allow me to type in the combobox.
Any solutions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有提供示例代码,并且 tkinter 没有提供默认的自动完成组合,我假设您正在使用
ttkwidgets.autocomplete
中的AutocompleteCombobox
。要仅获取有效条目(无自定义条目),您必须重新实现
AutocompleteCombobox
类的自动完成方法。逻辑很简单:检查当前用户输入是否在自动完成列表中。如果没有,请删除最后一个字符并再次显示最后一个自动完成建议。
我使用此源中的示例代码作为我的答案的基础。
以下是实现自定义
MatchOnlyAutocompleteCombobox
的代码片段:Since You didn't provide example code and tkinter does not provide default autocomplete combobx, I assumed You are using
AutocompleteCombobox
fromttkwidgets.autocomplete
.To get only valid entries (no custom one), You have to re-implement autocomplete method of
AutocompleteCombobox
class.Logic is simple: check if current user input is in autocomplete list. If not, remove last character and show again last autocomplete suggestion.
I used example code from this source as a base for my answer.
Here is code snippet implementing custom
MatchOnlyAutocompleteCombobox
: