1

I have constructed a listbox using tkinter Now I want the user to click on an item in the listbox that creates a variable with the selection.

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

def get_list(event):
    index = listbox.curselection()[0]
    seltext = listbox.get(index)
    print(seltext)

This properly prints out the selection. However, I am unable to get the "seltext" out of the function and able to use it later in the code. Someone recommended the get_list(event) however I don't know where event came from. Appreciate the help

EDIT:

   for f in filename:
                with open(f) as file_selection:
                    for line in file_selection:
                        Man_list = (line.split(',')[1])

                        listbox.insert(END, Man_list)

                file_selection.close()



        def get_list(event):
            global seltext
            # get selected line index
            index = listbox.curselection()[0]
            # get th e line's text
            global seltext
            seltext = listbox.get(index)



        # left mouse click on a list item to display selection
        listbox.bind('<ButtonRelease-1>', get_list)
Alex Weinstein
  • 69
  • 2
  • 10

1 Answers1

1

This is an issue with how you are structuring your code more than anything. Event handlers are not built to return values. Use a global variable or class property to retrieve and store the value. A global variable can be set like so:

from Tkinter import *

def get_list(event):
    global seltext
    index = listbox.curselection()[0]
    seltext = listbox.get(index)

root = Tk()
listbox = Listbox(root)
listbox.pack()
listbox.bind('<ButtonRelease-1>', get_list)

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)

seltext = None

root.mainloop()

But a class property is the better option. Your Tkinter application should be modular to ensure everything is easily accessible and prevent problems like this. You can set variables to self and access them later that way.

from Tkinter import *

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        listbox = Listbox(self)
        listbox.pack()
        listbox.bind('<ButtonRelease-1>', self.get_list)

        for item in ["one", "two", "three", "four"]:
            listbox.insert(END, item)

        self.listbox = listbox # make listbox a class property so it
                               # can be accessed anywhere within the class

    def get_list(self, event):
        index = self.listbox.curselection()[0]
        self.seltext = self.listbox.get(index) 

if __name__ == '__main__':
    app = Application()
    app.mainloop()
Graham
  • 7,431
  • 18
  • 59
  • 84
Hobbes
  • 404
  • 3
  • 9
  • for the first one, how would you get seltext out of the function? – Alex Weinstein Aug 15 '16 at 17:29
  • still unable to have print(seltext) outside of the function – Alex Weinstein Aug 15 '16 at 17:30
  • My entire answer covers how you would transfer a variable to another scope without the use of a return statement. You need to be more clear with what issue you are running into, and maybe post more code in the original post. It's hard to diagnose a scope issue without more information. – Hobbes Aug 15 '16 at 17:50
  • Your first code: you have seltext = None....I want this seltext to be equal to what was clicked. What is the best way to do this? – Alex Weinstein Aug 15 '16 at 17:54
  • `seltext = None` is declaring that the variable exists. When the user clicks and releases their mouse button on a listbox value, the code enters `get_list(event)`. Within `get_list(event)` a [global variable](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) is called and set to the value that the user selected. From this point on in the program, `seltext` will refer to the value selected until changed. – Hobbes Aug 15 '16 at 17:59
  • Later in the program if you do print(seltext) the following error message occurs – Alex Weinstein Aug 15 '16 at 18:02
  • NameError: name 'seltext' is not defined – Alex Weinstein Aug 15 '16 at 18:02
  • It completely depends on where you are trying to print `seltext`. You would need to declare `global seltext` at the top of any function that uses it. Once again, it is very difficult to diagnose scope issues without more code. These are two ways that you can access a variable outside of a function without the use of a return statement. It is up to you to understand the base concepts and modify them to fit your specific code. – Hobbes Aug 15 '16 at 18:05
  • I have attached the code above. Let me know if you can help out I really appreciate it! – Alex Weinstein Aug 15 '16 at 18:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120989/discussion-between-alex-weinstein-and-hobbes). – Alex Weinstein Aug 15 '16 at 18:23