6 Years
Discussion Span
Last Post by OlyDLG

but I need to return the result of the ButtonRelease-1 to main

I for one have no idea what this means as the URL referenced contains many examples.


So much for the usefulness of Permalink; here's the code to which I was referring. Thanks!

# create a scrollable listbox using Tkinter
# load the listbox with tasty cheese data
# and select your favorite cheese with the mouse
# with Python3 use import tkinter as tk

import Tkinter as tk

def get_list(event):
function to read the listbox selection
and put the result in a label widget
# get selected line index
index = listbox.curselection()[0]
# get the line's text
seltext = listbox.get(index)
# put the selected text in the label
label = seltext

root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)

# create a label (width in characters)
s = "Click on a cheese"
label = tk.Label(root, text= s, width=15)
label.grid(row=0, column=0)

# create a listbox (height in characters/lines)
# give it a nice yellow background (bg) color
listbox = tk.Listbox(root, height=15, bg='yellow')
listbox.grid(row=1, column=0)

# the Tkinter listbox does not automatically scroll, you need
# to create a vertical scrollbar to the right of the listbox
yscroll = tk.Scrollbar(command=listbox.yview, orient=tk.VERTICAL)
yscroll.grid(row=1, column=1, sticky='n'+'s')

cheese_list = [
'Feta', 'Limburger', 'Camembert', 'Roquefort', 'Edam',
'Romadur', 'Liptauer', 'Dubliner', 'Gouda', 'Gorgonzola',
'Jarlsberg', 'Golka', 'Garrotxa', 'Swiss', 'Quesillo',
'Emmentaler', 'Appenzeller', 'Raclette', 'Asiago', 'Zuvi',
'Ricotta', 'Mozzarella', 'Munster', 'Parmesan']

# load the listbox
for item in cheese_list:
listbox.insert('end', item)

# use left mouse click on a list item to display selection
listbox.bind('<ButtonRelease-1>', get_list)


This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.