Tkinter and Listbox – 2 (sync)

Some listboxes in a tkinter GUI

We were at this point in the last post about tkinter and listbox.

We had two listbox that could scroll using the mousewheel, the arrow keys or dragging the mouse over the listboxes.

# lb8
import tkinter as tk

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(100):
		lb.insert(tk.END, n)
	return lb

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

What if we wanted to scroll the two listboxes in sync?

Sync with the mousewheel

Now we sync the listbox scrolling with the mousewheel action.

This function makes a listbox (passed as argument to be moved of a step of four. But, what is that 120 thing? When the mousewheel moves its event.delta returns 120 or -120. If we divide this event.delta by 120 we will get 1 or -1, that multiplicated by 4 will give 4 or -4, so that the listbox will scroll up or down of 4.

def scroll(event, lb):
	lb.yview_scroll(int(-4*(event.delta/120)), "units")

This function must be binded to the listbox1 (lb1) and 2 (lb2). This is how:

lb1.bind("<MouseWheel>", lambda e: scroll(e, lb2))
lb2.bind("<MouseWheel>", lambda e: scroll(e, lb1))

Sync with arrowkeys

Now we want to sync also with the arrow key Up and Down.

We create this function

def scroll2(event, num):
	lb1.yview("scroll", int(num), "units")
	lb2.yview("scroll", int(num), "units")

and we bind the listboxes to the arrow Up and Down like this

lb2.bind("<Up>", lambda e: scroll2(e, -1))
lb1.bind("<Up>", lambda e: scroll2(e, -1))
lb2.bind("<Down>", lambda e: scroll2(e, 1))
lb1.bind("<Down>", lambda e: scroll2(e, 1))

The whole code

# lb8
import tkinter as tk

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(100):
		lb.insert(tk.END, n)
	return lb

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


###################################### 2
def scroll(event, lb):
	lb.yview_scroll(int(-4*(event.delta/120)), "units")
def scroll2(event, num):
	lb1.yview("scroll", int(num), "units")
	lb2.yview("scroll", int(num), "units")
lb1.bind("<MouseWheel>", lambda e: scroll(e, lb2))
lb2.bind("<MouseWheel>", lambda e: scroll(e, lb1))
###################################### /2
lb2.bind("<Up>", lambda e: scroll2(e, -1))
lb1.bind("<Up>", lambda e: scroll2(e, -1))
lb2.bind("<Down>", lambda e: scroll2(e, 1))
lb1.bind("<Down>", lambda e: scroll2(e, 1))
root.mainloop()

To be continued… ?

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.