Tkinter and listbox – 1

Let’s talk again about listbox… and tkinter, some of my favourite topics. As I do sometimes want to revisit code to make some basic examples of possible use, sometimes suggested by question that someone makes.

Create one or more listboxes with less code

Follow me in this live coding about listboxes and tkitner to see how you can use them writing less code. At the end of this live coding we will have 2 listboxes that will scroll in sync.

In the first part we just create 2 listboxes in a frame and we populate them with random items (numbers). We use the pack method to organize the widgets on the window. The frame is a container that we use to give the app a better layout, being the pack() method easy to use, but very limited to center, left and right positioning.

Importing stuff

We need tkinter and random (the second is just to fill the listboxes with random numbers).

import tkinter as tk
from random import randint

Window, title and dimensions

We… do what you can imagine reading the very intuitive code below:

root = tk.Tk()
root.title("Listboxes in Sync")
root.geometry("400x300+400+200")

A function to use less code to create the listboxes

As we are going to create 2 listboxes, we create a ‘shortcut’ reusing this code twice:

def def_listbox(): # => Listbox
	lb = tk.Listbox(frame1)
	lb.pack(side="left", expand=1, fill=tk.BOTH)

	for n in range(49):
		rnd = str(randint(1,100))
		lb.insert(tk.END, rnd)
	return lb

We created the listbox from an instance of Listbox (tk is the tkinter module, to have access to the module we have to write tk then a dot . and the the name of the class or function or attribute coming from the module; PS: we gave to tkinter the name of tk in the import line above).

This function returns a lb istance of Listbox. So each time we call this function a Listbox is created. To create more Listbox and have the chance to work on them you have to memorize the call to this function in a label like this:

lb1 = def_listbox()

If you want another Listbox, you just:

lb2 = def_listbox()

If we didn’t wrote the function, we should have written the same stuff in the function twice. So, we saved space and simplified the readability of the code (and also his mantainance).

We need to create the frame in wich the listboxes are contained (it is the argument frame1 in the first line of code of the function above.

The frame widget must be created before the listbox object, otherwise the interpreter will give us an error.

frame1 = tk.Frame(root)
frame1.pack(expand=1, fill=tk.BOTH)

The whole code for listboxes in tkinter

So, we’re done, add the mainloop and run this code:

# lb8
import tkinter as tk
from random import randint

root = tk.Tk()
root.title("Listboxes in Sync")
root.geometry("400x300+400+200")

def def_listbox(): # => Listbox
	lb = tk.Listbox(frame1)
	lb.pack(side="left", expand=1, fill=tk.BOTH)

	for n in range(49):
		rnd = str(randint(1,100))
		lb.insert(tk.END, rnd)
	return lb

frame1 = tk.Frame(root)
frame1.pack(expand=1, fill=tk.BOTH)
lb1 = def_listbox()
lb2 = def_listbox()

root.mainloop()

You will see this:

Live conding listbox tkinter

Second part of this post

Click on this link if you want to read the second part of this post.

Tkinter test for students

Tkinter articles

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.