Tkinter – Show files in the folder and the computer reads the content

The os module and listdir method

The aim of this app is to read a txt file chosen in a list of files that are present in a folder and to read the content with a synthetic voice (for Windows) when you press a button.

We want to make a practical example of coding with tkinter and listbox, populating the items with the files of a directory.

To get the files in the current directory, we’ll choose os.listdir, like in the code below

# os module


import os

# get the list of files
flist = os.listdir()

Create the listbox

Now we create the listbox

#

import tkinter as tk

win = tk.Tk()

lbox = tk.Listbox(win)
lbox.pack()

win.mainloop()

Populate the listbox with files

Now we unite the two codes:

#


import tkinter as tk
import os

# WINDOW CREATION
win = tk.Tk()
geo = win.geometry
geo("400x400+400+400")
win['bg'] = 'orange'
# get the list of files
flist = os.listdir()

lbox = tk.Listbox(win)
lbox.pack()

# THE ITEMS INSERTED WITH A LOOP
for item in flist:
    lbox.insert(tk.END, item)

win.mainloop()

The result is this:

Add a Text

In this video you can see how to add a Text in tkinter:

Add a Text, the <<ListboxSelect>> and read the file

 

#

import tkinter as tk
import os

# WINDOW CREATION
win = tk.Tk()
geo = win.geometry
geo("400x400+400+400")
win['bg'] = 'orange'

# get the list of files
flist = os.listdir()

lbox = tk.Listbox(win)
lbox.pack()

# THE ITEMS INSERTED WITH A LOOP
for item in flist:
    lbox.insert(tk.END, item)


def showcontent(event):
    x = lbox.curselection()[0]
    file = lbox.get(x)
    with open(file) as file:
        file = file.read()
    text.delete('1.0', tk.END)
    text.insert(tk.END, file)


text = tk.Text(win, bg='cyan')
text.pack()

lbox.bind("<<ListboxSelect>>", showcontent)

win.mainloop()

Add a button to read the content of files

Now, we want to make the computer to read the content of the files when we push a button. So we need the win32mdule (search on this site, I’ve made an article about it). Here is the code.

#


import tkinter as tk
import os
from win32com.client import Dispatch


s = Dispatch("SAPI.SpVoice")

# WINDOW CREATION
win = tk.Tk()
geo = win.geometry
geo("400x400+400+400")
win['bg'] = 'orange'

# get the list of files
flist = os.listdir()

lbox = tk.Listbox(win)
lbox.pack()

# THE ITEMS INSERTED WITH A LOOP
for item in flist:
    lbox.insert(tk.END, item)


def showcontent(event, audio=0):
    x = lbox.curselection()[0]
    file = lbox.get(x)
    with open(file, 'r', encoding='utf-8') as file:
        file = file.read()
    text.delete('1.0', tk.END)
    text.insert(tk.END, file)


def audio():
    s.Speak(text.get('1.0', tk.INSERT))


button = tk.Button(win, text="audio")
button['command'] = audio
button.pack()

text = tk.Text(win, bg='cyan')
text.pack()
# BINDING OF LISTBOX lbox
lbox.bind("<<ListboxSelect>>", showcontent)
# BUTTON

win.mainloop()

Video with program working

In the following video you can see the code in action.

The last touch: open the files

Finally, we want to add some code so that when we double click on a file name it opens:

Binding the double click to the listbox

# binding

lbox.bind("<Double-Button-1>", opensystem)

The function called by the double click event

# function called by the event listener
# this opens the files

def opensystem(event):
    x = lbox.curselection()[0]
    os.system(lbox.get(x))
# the whole code with the double click
# to open files in the listbox


import tkinter as tk
import os
from win32com.client import Dispatch


s = Dispatch("SAPI.SpVoice")

# WINDOW CREATION
win = tk.Tk()
geo = win.geometry
geo("400x400+400+400")
win['bg'] = 'orange'

# get the list of files
flist = os.listdir()

lbox = tk.Listbox(win)
lbox.pack()

# THE ITEMS INSERTED WITH A LOOP
for item in flist:
    lbox.insert(tk.END, item)


def showcontent(event, audio=0):
    x = lbox.curselection()[0]
    file = lbox.get(x)
    with open(file, 'r', encoding='utf-8') as file:
        file = file.read()
    text.delete('1.0', tk.END)
    text.insert(tk.END, file)


def audio():
    s.Speak(text.get('1.0', tk.INSERT))


def opensystem(event):
    x = lbox.curselection()[0]
    os.system(lbox.get(x))


button = tk.Button(win, text="audio")
button['command'] = audio
button.pack()

text = tk.Text(win, bg='cyan')
text.pack()
# BINDING OF LISTBOX lbox
lbox.bind("<<ListboxSelect>>", showcontent)
lbox.bind("<Double-Button-1>", opensystem)
# BUTTON

win.mainloop()

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.