Question

I'm studying the Tkinter Listbox widget and have been unable to find solutions for the following functionality:

  1. 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).

  2. How can I disable a specific item? I tried using .itemconfig( index, state='disabled' ) without success.

  3. 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?

  4. Would some of the above functionality be easier to implement using a Text widget or the ttk Treeview widget?

Thank you, Malcolm

Was it helpful?

Solution

  1. you cannot. The widget doesn't support that.

  2. 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.

  3. You will need to bind to keypress events and manage the behavior yourself. It's not particularly difficult, just a little tedious.

  4. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top