3

I am having a problem configuring a listbox widget such that the selection remains highlighted even while it is set (programmatically) to the DISABLED state. Below code shows the problem:

from Tkinter import *
master = Tk()
listbox = Listbox(master)
listbox.pack()
listbox.insert(END, "Text1")
listbox.insert(END, "Text2")
listbox.insert(END, "Text3")
listbox.selection_set(first=0, last=None)
listbox.configure(exportselection=False)
listbox.configure(state=DISABLED)

Now when I change state to NORMAL, selected item is being highlighted. Is there a way I could disable widget (i.e. No response on mouse clicks) but keep the selected object remain highlighted?

Intent: I want to utilise this widget on wizard App that I am creating. I would like this widget to indicate the current page / wizard number which the user selected. Is there any other widget I could use instead of it? (Labels possibly?)

gary
  • 4,227
  • 3
  • 31
  • 58
sarbjit
  • 3,786
  • 9
  • 38
  • 60

1 Answers1

7

You can leave it enabled but remove or override all of the default bindings; that would give you the effect that you want.

You can remove all the default bindings by removing the "Listbox" bindtag, or by adding your own bindings that override the default ones.

Here's how to set the bindtags so that all of the standard Listbox bindings are removed:

listbox.bindtags((listbox, master, "all"))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks Bryan, You have been very helpful. When i execute the above code, i got an error "Self not defined". Actually i am a beginner, so i will appreciate if you can provide some detailed explanation of the code that you posted above and some pointers for studying. I was looking at the bindtags command in some other post where you explained it very well through example. Thanks for that !! – sarbjit Jul 18 '12 at 04:59
  • In post "http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widg", you mentioned that the values will be visible only after class binding for first case. But we have already binded that entry widget with procedure. So when event occurs, that def should be called and value be visible. why it is lagging one character. Actually i couldn't get the reason. Can you please explain it as well. – sarbjit Jul 18 '12 at 05:38
  • @sarbjit: re: `self not defined`: my mistake. I was assuming this was in a class. replace `self` with `listbox`. The goal is to make the listbox widget the first item in the bindtags. I've updated the answer. – Bryan Oakley Jul 18 '12 at 11:08
  • @sarbjit: you seem to be asking a question in your comment related to a binding on an entry widget, but this question doesn't have an entry widget or any widget with a binding. Comments aren't for an extended discussion, they are for getting clarity on an answer. If you have another question, ask it as a proper question. – Bryan Oakley Jul 18 '12 at 11:11
  • I'll take care of it in future. Thx – sarbjit Jul 18 '12 at 12:23