4

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()

noah
  • 2,616
  • 13
  • 27

2 Answers2

2

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:

Tuhin Mitra
  • 555
  • 7
  • 19
  • i need ur help on this question https://stackoverflow.com/questions/66309595/tkinter-open-html-file-based-on-filename-in-dropdown-menu – NAzira Nasir Feb 22 '21 at 03:18
1

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

Tls Chris
  • 3,564
  • 1
  • 9
  • 24