How to display text of listbox items in the canvas with Python3x tkiner?
Question: I am trying to display the text of the selected item of a listbox into the
canvas. When I bind the listbox to a helper event handler, it throws Attribute
Error: CLASS object has no attribute "HELPER EVENT HANDLER".

What I want is as follows:

1) When double clicking an item in the listbox to the left, its text should be
displayed on the canvas. This particular line of code is causing all the
troulbe to me.

    
    
    lstbox.bind("<Double-Button-1>", self.OnDouble) 
    

Could you please help me fixing this error?

2) I believe that there must be a way to make the lines' height on the listbox
larger than they appear in my application. However, I don't know how to do it.
I tried providing several options but these options are not recognized by
tkinter. Could you please suggest to me how to do it?

Here is the code:

    
    
    import tkinter as tk
    languages = ['Mandarin', 'English', 'French']
    class LanguageFamilies(tk.Frame):
        def __init__(self, *args, **kwargs):
            tk.Frame.__init__(self, *args, **kwargs)
            canv = tk.Canvas(self, width=675, height=530, bg="white", relief="sunken")
            canv.config(scrollregion=(0,0,300,650), highlightthickness=0)
            canv.pack(side="right", expand=True, fill="both")
    
            # Create scroll bar
            sbar = tk.Scrollbar(self)
            canv.config(yscrollcommand=sbar.set)
            sbar.config(command=canv.yview)
            sbar.pack(side="right", fill="both")
    
            # Create Scroll List
            lstbox = tk.Listbox(self, width=240, height=530, relief="sunken", font="Courier")
            lst_scrollbar = tk.Scrollbar(self)
            lstbox.config(yscrollcommand=lst_scrollbar.set)
            lst_scrollbar.config(command=lstbox.yview)
            lstbox.pack(side="left", expand=True, fill="both")
            lst_scrollbar.pack(side="right", fill="both")
            lstbox.bind("<Double-Button-1>", self.OnDouble) # THIS IS THE LINE CAUSING THE ERROR
    
            # Add items to the lstbox
            i = 0
            while i < len(languages):
                lstbox.insert(i, languages[i])
                i += 1
    
            # Create a text inside canvas
            canv_id = canv.create_text(50, 50, font="Times 14", anchor="nw")
            msg = "This is a text inside canvas."
            canv.insert(canv_id, "end", msg)
    
            #Binding Handler
            def OnDouble(self, event):
                self.widget = event.widget
                selection = self.widget.curselection()
                content = self.widget.get(selection[0])
                print("You selected", content)
    
    
    
    if __name__ == "__main__":
        root = tk.Tk()
        root.geometry("930x530")
        root.title("Language Families")
        LanguageFamilies(root).pack(fill="both", expand=True)
        root.mainloop()
    

And this is the error message:

    
    
    Traceback (most recent call last):
    File "C:/Python33/new_stack_overflow.py", line 43, in <module>
    LanguageFamilies(root).pack(fill="both", expand=True)
    File "C:/Python33/new_stack_overflow.py", line 23, in __init__
    lstbox.bind("<Double-Button-1>", self.OnDouble)
    AttributeError: 'LanguageFamilies' object has no attribute 'OnDouble'
    

Your help is highly appreciated!


Answer: the problem is that the def for `OnDouble` is defined _inside_ `__init__`,
making it not a class method but a method inside the scope of `__init__`. You
need to remove one level of indentation for `OnDouble`.

