Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Tkinter to open file in folder using dropdown

I need help on how to make a button after I choose a folder in dropdown list.

For Example: I have 3 folders name "Folder 1","Folder 2" & "Folder 3". Inside "Folder 1", I have 5 excel(.xlsx) files. So I need help on how to read and display the data in 1 excel(.xlsx) file.

My current situation: I choose "Folder 1" in the dropdown menu. The next thing that I need is a button which can open the "Folder 1" and display the other list of 5 excel(.xlsx) files. And then, I can choose one of the excel(.xlsx) file and display the data inside the gui.

Here is my code.... Help me :'(


import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
folder = r'C:\Users\test folder'
filelist = [fname for fname in os.listdir(folder)]
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()

like image 461
NAzira Nasir Avatar asked Oct 30 '25 22:10

NAzira Nasir


2 Answers

You cannot just select and read a file's contents from tkinter. You have to write some other scripts for that reading part.

What the selection of filename does from tkinter combo box, is nothing but, get the particular file name as a string type.

However, in Python it's pretty straight forward to read a .xlsx file. You can use Pandas module for that.

I have written the code for you to read the file, (but you have to install pandas)

from functools import partial
import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas


def get_selected_file_name(file_menu):
    filename = file_menu.get()
    print("file selected:", filename)
    reader = pandas.read_excel(filename)  # code to read excel file
    # now you can use the `reader` object to get the file data


folder = os.path.realpath('./test_folder')
filelist = [fname for fname in os.listdir(folder)]
master = tk.Tk()
master.geometry('1200x800')
master.title('Select a file')
optmenu = ttk.Combobox(master, values=filelist, state='readonly')
optmenu.pack(fill='x')

button_select = tk.Button(master, text="Read File",
                          width=20,
                          height=7,
                          compound=tk.CENTER,
                          command=partial(get_selected_file_name, optmenu))
button_select.pack(side=tk.RIGHT)
master.mainloop()

The window should look somewhat like this:

like image 119
Tuhin Mitra Avatar answered Nov 01 '25 12:11

Tuhin Mitra


I'd explore using the filedialog module in tkinter.

import tkinter as tk
from tkinter import filedialog


def load_file():
    f_in = filedialog.askopenfilename( filetypes = [ ( 'Python', '*.py' ) ])  # Change to appropriate extension.
    if len( f_in ) > 0:
        with open( f_in, 'r' ) as file:
            filedata = file.read()
        print( filedata )   # printed to the terminal for simplicity.
                            # process it as required.

root = tk.Tk()

tk.Button( root, text = 'Find File', command = load_file ).grid()

root.mainloop()

askopenfilename allows a user to navigate the folder tree to find the correct file. Basic documentation

like image 42
Tls Chris Avatar answered Nov 01 '25 13:11

Tls Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!