Blue Sky
Blue Sky

Reputation: 78

How to list files in a folder to a TK Listbox - Python3

The title tells you everything, I found some list files in a folder, stackoverflow questions. But none of exactly what I want.

I would really appreciate your help. Here's some code to help structure your answers.

from tkinter import *
import tkinter.messagebox as box

window = Tk()
window.title( '<title>' )

frame = Frame( window )

listbox = Listbox( frame )
listbox.insert( 1, '<filename>' )
listbox.insert( 2, '<filename>' )
listbox.insert( 3, '<filename>' )

def dialog() :
    box.showinfo( 'Selection' , 'Your Choice: ' + \
    listbox.get( listbox.curselection() ) )

btn = Button( frame, text = 'View Info', command=dialog )

btn.pack( side = RIGHT , padx = 5 )
listbox.pack( side = LEFT )
frame.pack( padx = 30, pady = 30 )

window.mainloop()

Upvotes: 1

Views: 13206

Answers (3)

Walid Bousseta
Walid Bousseta

Reputation: 1469

A much more simple way is to use StringVar:

To populate items to a Listbox, you first create a StringVar object that is initialized with a list of items. And then you assign this StringVar object to the listvariable option as follows:

list_items = StringVar(value=items)
listbox = Listbox(root, listvariable=list_items)
listbox.pack()

To add, remove, or rearrange items in the Listbox, you just need to modify the list_items variable.

Upvotes: 0

314thon
314thon

Reputation: 21

I know the questions old but came across this and the above answer didn't work for me, found a solution though:

from tkinter import *
import os
...
myList = os.listdir('<folder location>')

myListBox = Listbox(<frame>)

for file in myList:
    MyListBox.insert(END, file)
MyListBox.pack()
...

this will only populate the listbox with the files from specified directory, once, when the page loads.

I was able to put this in a function and call the function once on page load, but then I added a button below the list to refresh on click. make your button(command=RefreshList)

...

def RefreshList():
    myList = os.listdir('<folder location>')
    print(MyList)
    MyListBox.delete(0, END)
    for file in MyList:
        MyListBox.insert(END, file)
RefreshList()
...

I think, you could make it automatically refresh when a new item is added to the directory, and I believe it would have to do with a loop, len() to count number of files in directory, then count number of items in list. compare the 2 with != and if True does nothing, elif RefreshList()

Alternatively, if you are saving files from your app, you could include "RefreshList()" at the end of your buttons save function, this way, once you save the new file to the directory, it will reload the directory contents into the listbox

Upvotes: 2

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19154

Since you do not know the number of items to insert when you write the program, replace

listbox = Listbox( frame )
listbox.insert( 1, '<filename>' )
listbox.insert( 2, '<filename>' )
listbox.insert( 3, '<filename>' )

with

listbox = Listbox(frame)
for name in files(dir):
    listbox.insert('end', name)

I leave it to you to replace files with the os directory-listing function call you want.

Upvotes: 1

Related Questions