[Tutor] problems using a listbox

Chris Roy-Smith chris_roysmith at internode.on.net
Tue Oct 17 02:25:04 EDT 2017


Hi,

OS: Linux Chris-X451MA 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 
17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Python 3.5.2 (default, Sep 14 2017, 22:51:06)

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). Also it doesn't 
print new values when I select an entry.

---------------------------------------------------

#!/usr/bin/python3
#test listbox
from tkinter import *

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)


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

listbox = Listbox(master)
listbox.grid(row=0)

for item in snames:
     listbox.insert(END, item)

myindicator=Dialog.list_has_changed(master, listbox)

mainloop()

-----------------------------------------

output:

---------------------------------------

./listbox.py

selection is .140537834621024

Thank you for any help

Regards, Chris Roy-Smith




More information about the Tutor mailing list