GUI: Création d’une ListBox avec un ScrollBar

Author:

 list, list, map, set, tkinter
{filelink=15422}

from Tkinter import *

class Scrolled_List(Frame):
    def __init__(self, options, parent=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)
        self.paintComponent(options)

    def list_event(self, event):
        """ Evénement """
        # Obtenir l'index de l'élément sélectionné
        index = self.listbox.curselection( )
        # obtenir le texte de l'élément sélectionné
        text = self.listbox.get(index)
        print 'Vous avez sélectionné', text

    def paintComponent(self, options):
        """ Création des Composants """
        scroll_bar = Scrollbar(self)
        list = Listbox(self, relief=FLAT)
        scroll_bar.config(command=list.yview)
        list.config(yscrollcommand=scroll_bar.set)
        scroll_bar.pack(side=RIGHT, fill=Y)
        list.pack(side=LEFT, expand=YES, fill=BOTH)
        index = 0
        for elem in options:
            list.insert(index, elem)
            index += 1
        list.config(selectmode=SINGLE, setgrid=1)
        list.bind('', self.list_event)
        self.listbox = list

if __name__ == '__main__':
    options = map((lambda x: 'a' + str(x)), range(26))
    Scrolled_List(options).mainloop( )

Livres Sur ce Sujet

[amazon_image id=”2212134347″ link=”true” target=”_blank” size=”medium” ]Apprendre à programmer avec Python 3[/amazon_image] [amazon_image id=”B005J2L260″ link=”true” target=”_blank” size=”medium” ]Apprenez à programmer en Python[/amazon_image] [amazon_image id=”2100508830″ link=”true” target=”_blank” size=”medium” ]Python : Petit guide à l’usage du développeur agile[/amazon_image] [amazon_image id=”2212127081″ link=”true” target=”_blank” size=”medium” ]Apprendre à programmer avec Python 3 : Avec plus de 50 pages de corigés d’exercices ![/amazon_image]

Leave a Reply

Your email address will not be published. Required fields are marked *