4

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Malcolm
  • 5,125
  • 10
  • 52
  • 75
  • I suggest that you'll get a better response if you ask these separately. – Steven Rumbalski Nov 10 '10 at 03:07
  • @Steven: I wasn't sure what the community posture was regarding how to chunk questions. I'll break up future posts into individual questions. – Malcolm Nov 10 '10 at 11:02
  • @Malcom I'm also not sure what the community thinks about it either. I read your question, thought I could answer a couple of them, but didn't because I couldn't answer the whole thing. – Steven Rumbalski Nov 10 '10 at 15:04
  • @Steven: If you have different answers than Bryan, then let me know and I will re-post my original question as separate questions. In the future I will post more granualar questions. – Malcolm Nov 10 '10 at 16:11
  • @Malcom: Bryan is entirely correct. – Steven Rumbalski Nov 10 '10 at 16:18

1 Answers1

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Thank you. Clever idea (2) for simulating disabledness. I'm going to search for a Unicode char as a better separator than the traditional dash or underscore. – Malcolm Nov 10 '10 at 11:01