[Tutor] problems using a listbox

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 17 05:49:18 EDT 2017


On 17/10/17 07:25, Chris Roy-Smith wrote:

> I am trying to learn how to use a tkinter listbox. When I execute my 
> experimental code, an odd index is printed immediately (output below 
> code), index looks wrong (shouldn’t it be an integer). 

You pass in the widget so thats what gets printed (the dot notation
is Tk's internal representation of the widget ID)

> Also it doesn't print new values when I select an entry.

I have no idea why you think it would, your code doesn't
even try to do that?


> class Dialog(Frame):
> 
>      def __init__(self, master):
>          Frame.__init__(self, master)
>          self.list = Listbox(self, selectmode=EXTENDED)
>          self.list.pack(fill=BOTH, expand=1)
>          self.current = None
>          self.poll() # start polling the list
> 
>      def poll(self):
>          now = self.list.curselection()
>          if now != self.current:
>              self.list_has_changed(now)
>              self.current = now
>          self.after(250, self.poll)
> 
>      def list_has_changed(self, selection):
>          print ("selection is", selection)

You define a class here but never create an instance of it.
Also it polls for changes in curselection - those will only
happen when you click on an item in the listbox in your dialog.

> snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry')
> master = Tk()
> 
> listbox = Listbox(master)
> listbox.grid(row=0)
> for item in snames:
>      listbox.insert(END, item)

And here you create a different listbox not in your
dialog and completely invisible to the code in your class.

> myindicator=Dialog.list_has_changed(master, listbox)

You are trying to call the method on the class passing
in the external listbox id as the selection value.

BTW. Polling is usually the wrong thing to do in
Tkinter, you should bind a method to the mouse
click event, something like:

self.list.bind('<ButtonRelease-1>', self.list_has_changed)

And modify list_has_changed to:

def list_has_changed(self):
    print ("selection is", self.list.curselection())

And you probably don't need the class, just use functions.
Here is what I think you wanted:

############################
from tkinter import *

def list_has_changed(event):  #bind requires an event parameter
    print ("selection is", listbox.curselection())

names=('fred', 'george', 'manuel', 'john', 'eric', 'terry')
master = Tk()

listbox = Listbox(master)
listbox.pack()
for item in names:
      listbox.insert(END, item)

listbox.bind('<ButtonRelease-1>', list_has_changed)

master.mainloop()
#########################

Note that curselection() always returns a tuple
since there could be multiple items elected.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list