I was trying to make the currently selected List box item to be printed out. For example, when i select item "one", it should print out "one" and when i select item "two", it should print out "two" etc. The following is what i have tried.
from Tkinter import*
sizex = 600
sizey = 400
posx = 40
posy = 20
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
for items in itemsforlistbox:
An item becomes active after you click on it—which means after your
ListboxSelect method returns. So, you're printing out whatever was active before this click (meaning, generally, what you clicked last time).
Also, given that you refer to "selected" numerous times, I think what you want is the selected value(s), not the active one, so you should be asking for that.
For a listbox with
BROWSE (the default, what you have) listbox, you can fix both of these trivially. Just change this:
If you need to handle
EXTENDED, then of course there are anywhere from 0 to 7 selections instead of exactly 1, so you need to do something like:
values = [mylistbox.get(idx) for idx in mylistbox.curselection()] print ', '.join(values)
While we're at it, I'm not sure why you were doing
str((mylistbox.get(ACTIVE))), or even
str(mylistbox.get(ACTIVE)). The result of
mylistbox.get with a single index is going to be a string, the same one you inserted.