Hi,
Gribouillis, helped me to understand how to invoke a function by btn click event handler, at my last post.Now I am trying to understand how I can handle an selected item event from a list box?
lets say I have a list box like below and I want to call a method to change the frame background color based on which item has been selected

from Tkinter import *

class App:
   def __init__(self, parent):
    self.myParent = parent
    self.fm = Frame(parent)
    self.fm.pack_propagate(0)
    self.fm.pack()

    self.Lb1 = Listbox(self.fm,height=4, width=15,selectbackground="orange")
    self.Lb1.insert(1, "Red")
    self.Lb1.insert(2, "Green")
    self.Lb1.insert(3, "Blue")
    self.Lb1.pack()

   def make_Red(self):
        self.fm.configure(bg="RED")

   def make_Blue(self):
        self.fm.configure(bg="BLUE")

   def make_Green(self):
        self.fm.configure(bg="GREEN")

root = Tk()
root.title ("Color Option")
root.geometry("%dx%d%+d%+d" % (300, 200, 0, 0))
app = App(root)
root.mainloop()

can you please take a look at this code and let me know how I can call one of the method by selecting the item from the list?
Thanks

I dont use Tkinter so much,wxpyhon is my favoritt gui-toolkit in python.
You can look at this post here show sneekula and vega the use of tkinter listbox.
http://www.daniweb.com/forums/thread191210-2.html

Thanks
these examples are good but they are too complicated!

Use curselection to return the item number selected. Note that the title of this thread indicates that this is a "HowTo", a code snippet on how to do something, and not a question. You will possibly get more views=more responses when it is clear that this is a question.

Hi woooee
Thanks for your hint.Honestly I know I should use the curselection and I also see some tutorials and codes regarding that but none of them invoke any methods when any item of the list was selected.
Thanks for your comment about title but It seems I can't edit it now but next time I will take care of that
Regards

It seems no body have any idea!

Ok I tried to modify the cod in this way:

from Tkinter import *

class App:
   def __init__(self, parent):
    self.myParent = parent
    self.fm = Frame(parent, width=400, height=300)
    self.fm.pack_propagate(0)
    self.fm.pack()

    self.Lb1 = Listbox(self.fm,height=4, width=15,selectbackground="orange")
    self.Lb1.insert(1, "Red")
    self.Lb1.insert(2, "Green")
    self.Lb1.insert(3, "Blue")
    self.cols = self.Lb1.curselection()
    if self.cols == 1:
        command=self.make_Red()
    if self.cols == 2:
        command=self.make_Red()
    if self.cols == 3:
        command=self.make_Red()

   def make_Red(self):

        self.fm.configure(bg="RED")

   def make_Blue(self):
        self.fm.configure(bg="BLUE")

   def make_Green(self):
        self.fm.configure(bg="GREEN")

root = Tk()
root.title ("Color Option")
root.geometry("%dx%d%+d%+d" % (300, 200, 0, 0))
app = App(root)
root.mainloop()

which did not call any function. I also tried this way

self.Lb1 = Listbox(self.fm,height=4, width=15,selectbackground="orange")
    self.Lb1.insert(1, "Red")
    self.Lb1.insert(2, "Green")
    self.Lb1.insert(3, "Blue")
    self.cols = self.Lb1.curselection()
 if self.cols == 1:
        self.make_Red()
    if self.cols == 2:
        self.make_Blue()
    if self.cols == 3:
        self.make_Green()
    self.Lb1.pack()

I am not getting any error but the event handler is not running neither.
Well It seems I am the only one interested to this topic!

You don't have a callback or any way to execute curselection. An example:

import Tkinter

class TestCallback:
   def __init__(self, top):
      self.top = top
      self.top.geometry( "100x100+10+10" )
      self.top.minsize( 200, 175 )

      self.listbox = Tkinter.Listbox( self.top, height=6, width=20, font=('Fixed', 14) )

      lit = [ "aaa", "bbbbb", "ccccccc", "dd", "e", \
              "fff", "ggggg", "hhhhhhh", "jj", "m", \
              "nn", "ooo", "ppp", "rr" "s" ]
      for item in range( len(lit) ):
          new_item = "%2d  %-10s" % (item, lit[item])
          self.listbox.insert(Tkinter.END, new_item)

      exit = Tkinter.Button(self.top, text='Exit',
             command=self.top.quit, bg='blue', fg='yellow' )
      exit.pack(side="bottom", fill=Tkinter.X, expand=1)


      self.listbox.pack(side="left")
      self.listbox.see(2)
      self.listbox.bind("<Double-Button-1>", self.test_callback)


   def test_callback(self, event):
      print self.listbox.curselection()
        
root = Tkinter.Tk()
TC = TestCallback(root)
root.mainloop()

Dear Wooee
Thanks for your hints again.I tried to update my code in this manner which is still not working!
I believe there is something wrong with my condition clauses!Could you please take a look at that and let me know how I can solve the issue?
I am really getting frustrated of this code!

from Tkinter import *

class App:
   def __init__(self, parent):
    self.myParent = parent
    self.fm = Frame(parent, width=400, height=300)
    self.fm.pack_propagate(0)
    self.fm.pack()

    self.Lb1 = Listbox(self.fm,height=4, width=15,selectbackground="orange")
    self.Lb1.insert(1, "Red")
    self.Lb1.insert(2, "Green")
    self.Lb1.insert(3, "Blue")
    self.Lb1.bind("<Double-Button-1>", self.call_back)

    self.Lb1.pack()

   def call_back(self, event):
     self.zones = self.Lb1.curselection()
     if self.zones == 1:
        self.make_Red()
     if self.zones == 2:
        self.make_Red()
     if self.zones == 3:
        self.make_Red()

   def make_Red(self, event):
        self.fm.configure(bg="RED")

   def make_Blue(self):
        self.fm.configure(bg="BLUE")

   def make_Green(self):
        self.fm.configure(bg="GREEN")

root = Tk()
root.title ("Color Option")
root.geometry("%dx%d%+d%+d" % (300, 200, 0, 0))
app = App(root)
root.mainloop()

Thanks for your time in advance

All your if branches self.Make_Red(), use the string value of selection for configure instead.

It works with

def call_back(self, event):
     self.zones = self.Lb1.curselection()
     assert len(self.zones) == 1
     z = self.zones[0]
     if z == '0':
        self.make_Red()
     elif z == '1':
        self.make_Green()
     elif z == '2':
        self.make_Blue()

Also, configure your editor to indent with 4 spaces, it will be easier to help.

commented: Perfect answer +1

I Think you are the only one knows TKinter!
Thanks man! It works fine now

It works with

def call_back(self, event):
     self.zones = self.Lb1.curselection()
     assert len(self.zones) == 1
     z = self.zones[0]
     if z == '0':
        self.make_Red()
     elif z == '1':
        self.make_Green()
     elif z == '2':
        self.make_Blue()

Also, configure your editor to indent with 4 spaces, it will be easier to help.

Thanks

Dont work for me in that way... the function curselection() returns a tuple not an integer. Like ('1',) So i made this work as follow

def call_back(self, event):
     self.zones = self.Lb1.curselection()
     if int(self.zones[0]) == 0:
        self.make_Red()
     if int(self.zones[0]) == 1:
        self.make_Blue()
     if int(self.zones[0]) == 2:
        self.make_Green()
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.