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)
5
  • Event handlers are not built to return values. Have you not tried making a global variable or a class property? Commented Aug 15, 2016 at 17:04
  • @Hobbes I am fairly new to Python so I really don't have an idea what those are. Are you able to help me? Commented Aug 15, 2016 at 17:05
  • What is the best way to do this? Commented Aug 15, 2016 at 17:18
  • stackoverflow.com/a/12936031/7432 Commented Aug 15, 2016 at 17:59
  • @BryanOakley thank you! Does this return the selected item back to the main program? Commented Aug 15, 2016 at 18:04

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

10 Comments

for the first one, how would you get seltext out of the function?
still unable to have print(seltext) outside of the function
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.
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?
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 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.