Tkinter:列表框分隔符、禁用项目、键盘导航?
我正在研究 Tkinter 列表框小部件,但无法找到以下功能的解决方案:
如何创建不可选择的水平分隔符项目,例如。分隔符相当于 Tkinter 菜单小部件的 .add_separator()? (使用破折号和下划线等字符看起来很糟糕)。
如何禁用特定项目?我尝试使用 .itemconfig(index, state='disabled') 但没有成功。
如何启用键盘导航,例如。当用户的键盘输入自动向前滚动到以用户键入的文本开头的最近的项目时?我必须
bind(
并自己管理此行为吗?, ...) 使用文本小部件或
ttk.Treeview
小部件可以更轻松地实现上述某些功能吗?
I'm studying the Tkinter Listbox widget and have been unable to find solutions for the following functionality:
How can I create non-selectable horizontal separator items, eg. separators equivalent to the Tkinter Menu widget's .add_separator()? (Using chars like dashes and underscores looks awful).
How can I disable a specific item? I tried using .itemconfig( index, state='disabled' ) without success.
How can I enable keyboard navigation, eg. when a user's keyboard input automatically scrolls one forward to the closest item that begins with the text the user typed? Must I
bind(<KeyPress>, ...)
and manage this behavior myself?Would some of the above functionality be easier to implement using a Text widget or the
ttk.Treeview
widget?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。该小部件不支持这一点。
您无法禁用某些项目,该小部件不支持状态属性。话虽这么说,您可以监视选择并在用户选择禁用的内容时执行适当的操作,并使用项目前景来表示禁用状态。
您需要绑定按键事件并自行管理行为。这并不是特别困难,只是有点乏味。
文本小部件可能是您最好的选择,尽管您必须添加绑定来模仿列表框的默认绑定。
底线:Tkinter 没有提供任何直接支持您想要做的事情,但构建块都在那里。您只需要自己构建它。
you cannot. The widget doesn't support that.
you can't disable certain items, the widget doesn't support a state attribute. That being said, you can monitor the selection and do the appropriate thing if the user selects something that is disabled, and use the item foreground to denote disabled-ness.
You will need to bind to keypress events and manage the behavior yourself. It's not particularly difficult, just a little tedious.
the text widget might be your best bet, though you'll have to add bindings to mimic the default bindings of the listbox.
Bottom line: Tkinter provides nothing that directly supports what you want to do, but the building blocks are all there. You'll just have to build it yourself.