1

I find this feature of automatic selection to item index 0 in the Listbox a nuisance. In my script, I have 3 listboxes called lb1,lb2 and lb3. If user selects any items on lb2 or lb3, I intend to pop up a MessageBox to ask the user to only select items from lb1. However, due to the automatic selection items index 0 in lb2 and lb3, whenever I click on item in lb1, the MessageBox also appears.

Question: How can I disable the initial selection of item index 0 in ListBox?

This is part of my script to call MessageBox if user selects items from lb2 or lb3:

if lb2.get(ACTIVE) or lb3.get(ACTIVE):
    tkMessageBox.showwarning("Warning","Please select from lb1 ")

Please advice! Any other ways to perform the intended action will also do. Thanks.

1
  • Thx for the edit. Very little view. Do I need to add other tags to this question ?
    – Poker Prof
    Jun 20, 2012 at 5:23

1 Answer 1

1

What OS are you using?

If I execute this code (taken from the Tkinter Listbox reference page on effbot.org) on Windows, there is no default selection made in the listbox.

from Tkinter import *
master=Tk()
listbox=Listbox(master)
listbox.pack()
for item in ['one','two','three','four']:
    listbox.insert(END, item)

EDIT : OK, now I see what you're asking. You want to check the curselection method first before trying to use get(ACTIVE).

if listbox.curselection():
    item = listbox.get(ACTIVE)

Does that help? You can find a more complete example here.

3
  • Hi there, I am using Windows XP Python 2.3. Ok what I mean from default selection is that, when I tried to retrieved the selected item in the listbox, automatically it will retrieve the 0th item even when I have not yet chosen/highlighted an item in the list box. I wish to remove this default chosen 0th item in the list.
    – Poker Prof
    Jun 20, 2012 at 14:21
  • Brandon, try use get function to get the item and you will see that although physically we do not see any item highlighted in default, in actuality, the 0th item will be obtained as default.
    – Poker Prof
    Jun 20, 2012 at 14:22
  • Ahah, why did I not think of just checking curlselection ! If user did not select any item, curlselection returns empty tuple. I shouldnt use get(ACTIVE) in the if statement. Thx for your help Brandon.Appreciate it.
    – Poker Prof
    Jun 21, 2012 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.