zebermeken
zebermeken

Reputation: 11

Python 3.6 - Create Dropdown for selecting files in a folder

Simply put, I want to have a GUI using Tkinter to open a dropdown window of a filder with 30+ CSV files so that I can select a single one of them and perform further actions on them after. Process order - Open directory -> Create Dropdown selection list -> select file -> Python selects file for further processing. So far I've been trying to mix the functions of Tkinter and os but with no luck.

import tkinter as tkr
import select
import sys
import os
#Basic idea of code with Os

print(' <select name="name">')
os.chdir("C:/Users/name/Desktop/folder")
for files in os.listdir("."):
    if files.endswith(".csv"):
        print('<option value="C:/users/'+files+'">'+files.replace('.csv','')+'</option>')    

#Understanding of Tkinter so far
master = tkr.Tk()
master.geometry("800x1200")
master.title("Select a File")

I know I need to find a way to set each CSV file as a variable and then assign them values for Tkinter to recognize and form a list from, but i don't have any clue how to do that.

Thank you.

Upvotes: 0

Views: 3338

Answers (1)

acw1668
acw1668

Reputation: 46678

You can use ttk.Combobox as the dropdown selection:

import os
import tkinter as tk
from tkinter import ttk

folder = 'C:/Users/name/Desktop/folder'
filelist = [fname for fname in os.listdir(folder) if fname.endswith('.csv')]

master = tk.Tk()
master.geometry('1200x800')
master.title('Select a file')

optmenu = ttk.Combobox(master, values=filelist, state='readonly')
optmenu.pack(fill='x')

master.mainloop()

Upvotes: 1

Related Questions