Home > front end >  tkinter display file name selected as text label in grid
tkinter display file name selected as text label in grid

Time:07-02

I'm new to Python and I'm trying to display the name of a file selected in a tkinter window using a grid format.

the code I have managed to put together so far is as follows:

from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile 
import time

ws = Tk()
ws.title('Select Input Files')
ws.geometry('400x300') 


def open_file():
    file_path = askopenfile(mode='r', filetypes=[('CSV Files', '*.csv')])
    if file_path is not None:
        pass

up1 = Label(
    ws, 
    text='sales_history_document '
    )
up1.grid(row=0, column=0, padx=10)

btn1 = Button(
    ws, 
    text ='Choose File', 
    command = lambda:open_file()
    )
btn1.grid(row=0, column=1)

up2 = Label(
    ws, 
    text='style_group_document '
    )
up2.grid(row=1, column=0, padx=10)

btn2 = Button(
    ws, 
    text ='Choose File ', 
    command = lambda:open_file()
    ) 
btn2.grid(row=1, column=1)

upld = Button(
    ws, 
    text='Upload Files', 
    command=uploadFiles
    )
upld.grid(row=7, columnspan=3, pady=10)

ws.mainloop()

The output is a series of labels and buttons but I would like to have the file name populate to the left to each button.

view output here

CodePudding user response:

Hey you are almost there. askopenfile gets the filename for you and you can use the name information like in the example below. I used the os package in addition:

from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile 
import time
import os

ws = Tk()
ws.title('Select Input Files')
ws.geometry('400x300') 


def open_file():
    file_path = askopenfile(mode='r', filetypes=[('CSV Files', '*.csv')])

    if file_path is not None:
        # this gets the full path of your selected file
        filename = file_path.name
        # this is only selecting the name with file extension
        filename = os.path.basename(filename)
        # to replace your Label you first have to destroy the other label
        up1.destroy()
        # then create a new label with the filename
        file_label = Label(ws, text=filename).grid(row=0, column=0, padx=10)
        # you can even change the title of your tkinter window if you want
        ws.title('Select Input Files -- '   filename)

up1 = Label(
    ws, 
    text='sales_history_document '
    )
up1.grid(row=0, column=0, padx=10)

btn1 = Button(
    ws, 
    text ='Choose File', 
    command = lambda:open_file()
    )

btn1.grid(row=0, column=1)

up2 = Label(
    ws, 
    text='style_group_document '
    )
up2.grid(row=1, column=0, padx=10)

btn2 = Button(
    ws, 
    text ='Choose File ', 
    command = lambda:open_file()
    ) 
btn2.grid(row=1, column=1)

ws.mainloop()

CodePudding user response:

If you just want to get the filename, use askopenfilename() instead of askopenfile().

You need to pass the label widget to open_file() if you want to show the filename in the label at the left of the button:

...
from tkinter.filedialog import askopenfilename
...
def open_file(lbl):
    file_path = askopenfilename(filetypes=[('CSV Files', '*.csv')])
    if file_path is not None:
        lbl.config(text=file_path)

up1 = Label(
    ws,
    text='sales_history_document '
    )
up1.grid(row=0, column=0, padx=10)

btn1 = Button(
    ws,
    text ='Choose File',
    command = lambda:open_file(up1)
    )
btn1.grid(row=0, column=1)

up2 = Label(
    ws,
    text='style_group_document '
    )
up2.grid(row=1, column=0, padx=10)

btn2 = Button(
    ws,
    text ='Choose File ',
    command = lambda:open_file(up2)
    )
btn2.grid(row=1, column=1)
...
  • Related