The listbox in tkinter
A listbox is a text box in with there are items in each row, like in a grocery list. I will show you the use of listbox in tkinter trough an example. This is the 11th lesson, so you should know about tkinter, the button widget and the entry widget. If not, go to the end of this article and check for the previous lessons.
An app to create a to do list in Python with Tkinter
To show you how to use the listbox, I will show you the code for a little app. In the video at the end of this article, I will explain the code, while I’m writing it.
The code of the App
Here is the full code. I’ll explain it in the next video.
import tkinter as tk def clicked(): listbox.insert(tk.END, content.get()) def delete(): listbox.delete(0, tk.END) def delete_selected(): listbox.delete(tk.ANCHOR) # The window root = tk.Tk() root.title("List App") root.geometry("400x400") # The entry to input the items content = tk.StringVar() entry = tk.Entry(root, textvariable=content) entry.pack() # The button to insert the item in the list button = tk.Button(root, text="Add Item", command=clicked) button.pack() # the button to delete everything button_delete = tk.Button(text="Delete", command=delete) button_delete.pack() # The button to delete only the selected item in the list button_delete_selected = tk.Button(text="Delete Selected", command=delete_selected) button_delete_selected.pack() # The listbox listbox = tk.Listbox(root) listbox.pack() root.mainloop()
Using a class for the app
We could use a class for this app to make a more mantainable code
import tkinter as tk # LISTBOX class Window: def __init__(self): self.root = tk.Tk() self.root.title("List App") self.root.geometry("400x400") self.widgets() def widgets(self): "Widgets in order from top to bottom" self.entry() self.buttons() self.listbox() def entry(self): "Here you input the items" self.content = tk.StringVar() self.entry = tk.Entry(self.root, textvariable=self.content) self.entry.pack() self.entry.focus() def buttons(self): "Buttons to insert the items, delete all of them or the selected one" self.button = tk.Button(self.root, text="Add Item", command=self.insert_item) self.button.pack() self.button_delete = tk.Button(self.root, text="Delete", command=self.delete) self.button_delete.pack() self.button_delete_selected = tk.Button(self.root, text="Delete Selected", command=self.delete_selected) self.button_delete_selected.pack() def listbox(self): "The listbox with the items" self.listbox = tk.Listbox(self.root) self.listbox.pack() self.root.mainloop() def insert_item(self): "The command to insert the item in the entry" self.listbox.insert(tk.END, self.content.get()) self.content.set("") def delete(self): "Command to delete all items" self.listbox.delete(0, tk.END) def delete_selected(self): "Command to delete only the selected item" self.listbox.delete(tk.ANCHOR) Window()
Video – part 1 and part 2
A better look to the GUI
If you want, you can use ttk to make some nice buttons
import tkinter as tk import tkinter.ttk as ttk # LISTBOX class Window: def __init__(self): self.root = tk.Tk() self.root.title("List App") self.root.geometry("400x400") self.widgets() def widgets(self): "Widgets in order from top to bottom" self.entry() self.buttons() self.listbox() def entry(self): "Here you input the items" self.content = tk.StringVar() self.entry = ttk.Entry(self.root, textvariable=self.content) self.entry.pack() self.entry.focus() def buttons(self): "Buttons to insert the items, delete all of them or the selected one" self.button = ttk.Button(self.root, text="Add Item", command=self.insert_item) self.button.pack() self.button_delete = ttk.Button(self.root, text="Delete", command=self.delete) self.button_delete.pack() self.button_delete_selected = ttk.Button(self.root, text="Delete Selected", command=self.delete_selected) self.button_delete_selected.pack() def listbox(self): "The listbox with the items" self.listbox = tk.Listbox( self.root) self.listbox.pack() self.root.mainloop() def insert_item(self): "The command to insert the item in the entry" self.listbox.insert(tk.END, self.content.get()) self.content.set("") def delete(self): "Command to delete all items" self.listbox.delete(0, tk.END) def delete_selected(self): "Command to delete only the selected item" self.listbox.delete(tk.ANCHOR) Window()
That’s what we got
Save the entries
In this code there is a way to save the data into a file, so that when you close the window with a button at the bottom of the window, it will save the data in the listbox. When you will open the window again, the data will be still there.
import tkinter as tk root = tk.Tk() root.title("List App") root.geometry("400x400") def retrievedata(): "Loads the data at the opening" global list_data list_data = [] try: with open("save.txt", "r", encoding="utf-8") as file: for f in file: listbox.insert(tk.END, f.strip()) list_data.append(f.strip()) print(list_data) except: pass def clicked(): "When click on a button it adds what is inside the entry box" global list_data listbox.insert(tk.END, content.get()) list_data.append(content.get()) def delete(): "Delete all the items in the list" global list_data listbox.delete(0, tk.END) list_data = [] def delete_selected(): "Delete the selected file" global list_data selected = listbox.get(listbox.curselection()) listbox.delete(tk.ANCHOR) #index = list_data[list_data.index(selected)] #print(index) list_data.pop(list_data.index(selected)) def quit(): "action performed when you click the button quit and save" global root with open("save.txt", "w", encoding="utf-8") as file: for d in list_data: file.write(d + "\n") root.destroy() # LISTBOX - The building of the GUI # The entry content = tk.StringVar() entry = tk.Entry(root, textvariable=content) entry.pack() # The button to add items linked to the clicked() function button = tk.Button(root, text="Add Item", command=clicked) button.pack() # the delete button, to delete all -> delete() button_delete = tk.Button(text="Delete", command=delete) button_delete.pack() # the button to delete only selected item button_delete_selected = tk.Button(text="Delete Selected", command=delete_selected) button_delete_selected.pack() # The listbox listbox = tk.Listbox(root) listbox.pack() # the button to quit and save the data in the listbox so that they will stay bquit = tk.Button(root, text="Quit and save", command=quit) bquit.pack() # the function to load the data saved previously retrievedata() # the built-in function to start the loop of the window root.mainloop()
This is how the window looks like. Remember to use the butto “Quit and save” to save the data.
Tkinter test for students
Tkinter articles
[hooops name=”all”]