Tkinter: Listbox separators, disabled items, keyboard navigation?
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 t开发者_开发知识库he 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?
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.
精彩评论