How To Put A List In A Listbox With Tkinter To Open Them With Double Click

This was made to help you to make things faster. I had this need to look into different folders I made in which there is an app that collects scripts or other files related to a topic. I save this stuffs into files that are called like the object I am collecting with _book at the end to distinguish them (i.e. pygame_book, python_book, tkinter_book etc.). I decided to make this script so that I can easily see the books I created among the other folders, because I have stored them in the main root of the hard drive and not in a folder (it could have been a good idea). So I create this sort of browser of selected folders so that I can see them separated by all the other folders in the root and I can also open them, using the bind method with the “<Double-Button>” argument, so that I open the relative folder in the list when I double click on it. As usual this post is made to make some simple app to make you understand how the widgets works together with binding methods with a practical example, rather with just a theric explanation of tkinter.

import os
from glob import glob
import tkinter as tk 


# Good code does not need comments... apart this!

def get_books_folders() -> list:
	""" returns all folders ending with _book, cause they got the files I need to run """
	folders = glob("*_book")
	print(folders)
	return folders

def openfolders(f):
	os.startfile(f)


def list_of_books():
	for f in folders:
		lbx.insert(0, f)


# GLOBALS
folders = get_books_folders()
root = tk.Tk()
root.geometry("400x600")
lbx = tk.Listbox()
lbx.bind("<Double-Button>", lambda x: openfolders(lbx.get(lbx.curselection())))
lbx.pack(fill="both", expand=1)
list_of_books()
root.mainloop()

Make the code reusable and readable

As I like to make the code more readable and possibly reusable, I made some changes to the code above that you can see here below.

import os
from glob import glob
import tkinter as tk 


# Good code does not need comments... apart this! and another couple below

def get_folders_list(_root, end) -> list:
	""" returns all folders ending with _book, cause they got the files I need to run """
	folders = sorted(glob(_root + "\\*" + end), reverse=True)
	# print(folders)
	return folders


def openfolders(f):
	""" Just launch a file """
	os.startfile(f)


def insert_filenames_in_listbox(folders, lbx):
	""" Get a list and insert it into a listbox """
	for f in folders:
		lbx.insert(0, f)

class FileList:
	""" Window with listbox, you pass a list to populate a listbox, double click to open files of list """
	def __init__(self, folders):
		root = tk.Tk()
		root.geometry("400x600")
		lbx = tk.Listbox()
		lbx.bind("<Double-Button>", lambda x: openfolders(lbx.get(lbx.curselection())))
		lbx.pack(fill="both", expand=1)
		insert_filenames_in_listbox(folders, lbx)
		root.mainloop()

# this gets the list of folders ending with...
folders = get_folders_list(_root="G:", end="_book")
# creates the window with the listbox with filename to click on
FileList(folders)

A simple class FileList to show files, but in a more simple way

I made this before… so I thought it could be interesting to compare this code with the previous one.

from glob import glob
import tkinter as tk
import sys

class FileList:
	def __init__(self, ft):
		root = tk.Tk()
		root.geometry("400x800")
		lb = tk.Listbox(root)
		lb.configure(bg="gold")
		lb.pack(fill="both", expand=True)
		fl = glob("*." + ft)
		for f in fl:
		    lb.insert(0,f)
		root.mainloop()


if __name__ == '__main__':
	FileList("py")

This is simpler, but hasn’t got the bind to the double click of the mouse on the file to open it. Maybe you can use it to do different things with the files.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

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.