Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

python - 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:Usersest 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()

question from:https://stackoverflow.com/questions/65880010/how-to-use-tkinter-to-open-file-in-folder-using-dropdown

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...