Press "Enter" to skip to content

How to Create List Box Using Tkinter in Python?

Here I am giving an example to create a list box using Tkinter in Python.

Python – Create List Box Using Tkinter Example

In the following example, it will create a list box and will create three elements in it, and when the user selects any element it will print on the screen.

import tkinter
from tkinter import simpledialog

class ChoiceDialog(simpledialog.Dialog):
    def __init__(self, parent, title, text, items):
        self.selection = None
        self._items = items
        self._text = text
        super().__init__(parent, title=title)

    def body(self, parent):
        self._message = tkinter.Message(parent, text=self._text, aspect=400)
        self._message.pack(expand=1, fill=tkinter.BOTH)
        self._list = tkinter.Listbox(parent)
        self._list.pack(expand=1, fill=tkinter.BOTH, side=tkinter.TOP)
        for item in self._items:
            self._list.insert(tkinter.END, item)
        return self._list

    def validate(self):
        if not self._list.curselection():
            return 0
        return 1

    def apply(self):
        self.selection = self._items[self._list.curselection()[0]]

if __name__ == '__main__':
    tk = tkinter.Tk()

    dialog = ChoiceDialog(tk, 'Pick one',
                          text='Please, pick a choice?',
                          items=['first', 'second', 'third'])
    print('Selected "{}"'.format(dialog.selection))


Create List box using Tkinter in Python ExampleSelected "third"