Hi,
I'm working on Python 2.4 and use tkinter to build my GUI.
I want to display content(line) from the listbox only when it is selected.

def get_list(event):
   # get selected line index
   index = Listbox.curselection()[0]
   # get the line's text
   seltext = Listbox.get(index)
   print seltext
   Listbox.bind('<ButtonRelease-1>', get_list)

This is the code I'm using but it's not working. Can you tell me why? I don't know if the mouse click is not getting sensed at all or I am not writing this function at the right location or the code itself is wrong.

Thanks

I you want to use Tkinter then you should also install the Pmw extension. It makes things much easier IMHO, and is a collection of Python/Tkinter files that extend Tkinter. That means it is easy to install as it is installed by copying to anywhere in your PYTHONPATH (sys.path). This is how it would be done with Pmw. Click on any item to print that item to the console.

import Tkinter
import Pmw

class PMWListBox :
   def __init__( self, list_in, min_width=300, min_height=300 ) :
      self.top = Tkinter.Tk()
      self.top.geometry( "300x300+10+10" )
      self.top.minsize( min_width, min_height )
      self.top.option_add('*Font', 'Fixed 14')
      
      Pmw.initialise( fontScheme='default')

      #---  exit button must be first----------------------
      exit = Tkinter.Button(self.top, text='EXIT',
             command=self.top.quit, bg='lightblue', fg='yellow' )
      exit.pack(fill="x", expand=1, side="bottom")

      #----------------------------------------------------------------
      self.sl_box = Pmw.ScrolledListBox( self.top, listbox_selectmode="SINGLE", \
               items=(list_in), selectioncommand=self.getit_2 )
      self.sl_box.pack(fill="both", expand=1, padx=5, pady=5, side="top" )
  
      #----------------------------------------------------------------
      self.top.mainloop()

   def getit_2(self):
#      print "getvalue", self.sl_box.getvalue()
      print "getcurselection", self.sl_box.getcurselection()

##===================================================================
if __name__ == "__main__" :
   list_items = [ "First List Entry", "Second List Entry", \
                  "Third List Entry", \
                  "Fourth List Entry -- W I D E -- W  I  D  E", \
                  "Fifth List Entry", "Sixth Entry", "Seventh Entry", \
                  "Eighth Entry", "Ninth Entry", "Tenth Entry", \
                  "Entry # 11 Test", "Test for 12th Lit", \
                  "Thirteen", "Fourteen", "Fifteen" ]
   PM = PMWListBox( list_items )
commented: great and stylish +13

Here is a typical example ...

# tk_Listbox_select1.py

import Tkinter as tk

def on_click_listbox(event):
    # get selected line index
    index = listbox1.curselection()
    # get the line's text
    seltext = listbox1.get(index)
    # show slected text in label
    label1.configure(text=seltext)

root = tk.Tk()

listbox1 = tk.Listbox(root, width=8, height=7)
listbox1.pack()

mylist = ['green', 'blue', 'red', 'gold', 'pink']
# load the listbox
for color in mylist:
    listbox1.insert('end', color)

label1 = tk.Label(root, text='click color to select', width=20)
label1.pack()

# use left mouse click on a list item to display selection
listbox1.bind('<ButtonRelease-1>', on_click_listbox)

root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.