GUI listbox

Post here if you need help with creating a Graphical User Interface in Python.

GUI listbox

Postby flyers2003go » Mon Aug 15, 2016 5:17 pm

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.
Code: Select all
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
Last edited by Yoriz on Mon Aug 15, 2016 7:21 pm, edited 1 time in total.
Reason: Please use code tags when posting code.
flyers2003go
 
Posts: 4
Joined: Fri Aug 12, 2016 5:09 pm

Re: GUI listbox

Postby Larz60+ » Wed Aug 17, 2016 10:54 am

Hello,

Here's an example I picked up somewhere (not my code) that does this:

Code: Select all
"a simple customizable scrolled listbox component"

from tkinter import *

class ScrolledList(Frame):
    def __init__(self, options, parent=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)                   # make me expandable
        self.makeWidgets(options)

    def handleList(self, event):
        index = self.listbox.curselection()                # on list double-click
        label = self.listbox.get(index)                    # fetch selection text
        self.runCommand(label)                             # and call action here
                                                           # or get(ACTIVE)
    def makeWidgets(self, options):
        sbar = Scrollbar(self)
        list = Listbox(self, relief=SUNKEN)
        sbar.config(command=list.yview)                    # xlink sbar and list
        list.config(yscrollcommand=sbar.set)               # move one moves other
        sbar.pack(side=RIGHT, fill=Y)                      # pack first=clip last
        list.pack(side=LEFT, expand=YES, fill=BOTH)        # list clipped first
        pos = 0
        for label in options:                              # add to listbox
            list.insert(pos, label)                        # or insert(END,label)
            pos += 1                                       # or enumerate(options)
       #list.config(selectmode=SINGLE, setgrid=1)          # select,resize modes
        list.bind('<Double-1>', self.handleList)           # set event handler
        self.listbox = list

    def runCommand(self, selection):                       # redefine me lower
        print('You selected:', selection)

if __name__ == '__main__':
    options = (('Lumberjack-%s' % x) for  x in range(20))  # or map/lambda, [...]
    ScrolledList(options).mainloop()


Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: GUI listbox

Postby llanitedave » Sun Aug 21, 2016 5:19 pm

flyers2003go wrote: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.
Code: Select all
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


Your "seltext" is a local variable that exits only inside the function get_list().

If the function is part of a class, and it should be, then you can define your variable as self.seltext. Thereafter, you can use it as such elsewhere in the class, or outside the class as "classname.seltext".
llanitedave
 
Posts: 4
Joined: Sun Dec 22, 2013 3:57 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests