Function to print directory on a Python Label

Asked

Viewed 177 times

0

I created a function that opens a file with Tkinter + filedialog, so far so good.

But I need to print the directory of the selected file on a label.

Follows function:

from tkinter import *
from tkinter import filedialog
import pandas as pd

def browse_button2():
    df2 = pd.read_csv(filedialog.askopenfile()) #aqui o botão seleciona o arquivo
    lb2["text"] = filedialog.askopenfilename() #aqui estou tentando definir que o label receba em sua propriedade "text" como valor a caminho do arquivo selecionado, Exemplo "C:\documentos\texte.csv"
    return df2

lb2 = Label(root, font=( 'arial', 10), bd=1, width=43, height=1, relief=RIDGE, borderwidth=1)
lb2.grid(row=2, column=0)
lb2.configure(background='#FFFFFF')
lb2.place(x=48, y=136)

Segue Imagem do sistema

1 answer

0

To insert a text into Label use the method config and pass the directory obtained as argument for the parameter text. The Label object also works as a dictionary, so you can also set the key text of the object with the directory: Example:

label.config(text=path)
label["text"] = path

Your program is opening the dialog box twice because you are calling askopenfile() in the first line of the function and then you are calling askopenfilename() in the third row.

Maybe what you want is to use the directory of the selected file in filedialog.askopenfile() to insert into the label. To do this, just use the attribute name to get the file directory:

def browse_button2():
    file = filedialog.askopenfile() # Recebo o arquivo selecionado
    df2 = pd.read_csv(file)
    lb2["text"] = file.name # Estou passando o diretório do arquivo selecionado
    return df2
  • to print the directory I used the following lb2["text"] = filedialog.askopenfilename() it sure prints the file directory, but it opens 2x the dialog to select a file

  • I understand, I’m going to try to explain it in a better way. to open the file I am using askopenfile(), which opens me a window to choose the file. I am not discrediting in the code a fixed path to choose the file. when therefore when I use the command of the form you sent me it prints the path of the function "Path" and not the path of the arch I selected

  • I think I understand what you want so I’ll edit the answer. Please see if that’s right.

  • Very good, that’s right, very much Thank you, I’m new with programming, this is very basic but for those who start sometimes enrroca in silly things

Browser other questions tagged

You are not signed in. Login or sign up in order to post.