listbox

Avec de la couleur

_images/20100811_1.jpg
from tkinter import *
from tkinter import ttk

# identify variables
countrynames = ('Argentina', 'Australia', 'Belgium', 'Brazil', 'Canada', \
        'China', 'Denmark', 'Finland', 'France', 'Greece', 'India', \
        'Italy', 'Japan', 'Mexico', 'Netherlands', 'Norway', 'Spain', \
        'Sweden', 'Switzerland')

def showPopulation(*args):
    print(countrynames[int(lbox.curselection()[0])])

#generate gui
root = Tk()
cnames = StringVar(value=countrynames)

c = ttk.Frame(root)
c.pack(fill=BOTH, expand=1)

s = Scrollbar(c)
lbox = Listbox(c, listvariable=cnames, height=5)
lbox.pack(side=LEFT, fill=BOTH, expand=1)
s.pack(side=RIGHT, fill=Y)

s.config(command=lbox.yview)
lbox.config(yscrollcommand=s.set)

lbox.bind('<<ListboxSelect>>', showPopulation)

# Colorize alternating lines of the listbox
for i in range(0,len(countrynames),2):
    lbox.itemconfigure(i, background='#f0f0ff')

lbox.selection_set(0)
root.mainloop()