0

I'm trying the implement the browse functionality in Tkinter, I'm able to implement the browse file option, however, after selecting the file at a particular location it's displaying file location on the console, how to print that location on the label? At the following place, file_location or file_name should be printed.

entry_1.insert(0, 'File_location')

For e.g. the file location is

Path with file name C:\Users\Desktop\test\test.txt

, so this file location should be printed on the label instead of the console.

And along with that, if I want only a path without a file name then how it can be done?

Path without file name. C:\Users\Desktop\test

What extra functionality do I need to add in order to implement this feature? Any help would be appreciated.

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from io import StringIO
import sys
import os 

root = Tk()
root.geometry('700x650')
root.title("Data Form")

def file_opener():
   input = filedialog.askopenfiles(initialdir="/")
   print(input)
   for i in input:
      print(i)

label_1 = Label(root, text="Location",width=20,font=("bold", 10))
label_1.place(x=65,y=130)

x= Button(root, text='Browse',command=file_opener,width=6,bg='gray',fg='white')
x.place(x=575,y=130)

entry_1 = Entry(root)
entry_1.place(x=240,y=130,height=20, width=300)
entry_1.insert(0, 'File_location')

root.mainloop()
  • About changing the label text https://stackoverflow.com/questions/17125842/changing-the-text-on-a-label. About removing the filename from the path, you should read the [documentation of the `os.path` library](https://docs.python.org/3/library/os.path.html#module-os.path) – j_4321 Mar 10 '21 at 10:19
  • just replace `print(...)` by `label_1.configure(...)` – j_4321 Mar 10 '21 at 11:29
  • Hey, I'm so sorry for the confusion, I had posted the wrong code, now it's updated, actually, I want the file location/name at 'entry_1.insert(0, 'File_location'). Could you be kind enough to update? –  Mar 10 '21 at 12:07

1 Answers1

0

Instead of using

filedialog.askopenfile(initialdir="/")

use

filedialog.askopenfilename(initialdir="/")

if you want to select multiple files and get a tuple as the input use

filedialog.askopenfilenames(initialdir="/")

And for the path without the file name use this

import os
os.path.dirname(input)

you can Insert the text to the Entry by using this

Updated:

entry_1.delete(0, END)
entry_1.insert(0, input[0])

Full Modified code (Updated:)

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from io import StringIO
import sys
import os


root = Tk()
root.geometry('700x650')
root.title("Data Form")


def file_opener():
    # input = filedialog.askopenfilenames(initialdir="/") # for selecting multiple files
    input = filedialog.askopenfilename(initialdir="/")
    entry_1.delete(0, END)
    entry_1.insert(0, input)


label_1 = Label(root, text="Location", width=20, font=("bold", 10))
label_1.place(x=65, y=130)

x = Button(root, text='Browse', command=file_opener, width=6, bg='gray', fg='white')
x.place(x=575, y=130)

entry_1 = Entry(root)
entry_1.place(x=240, y=130, height=20, width=300)
entry_1.insert(0, 'File_location')

root.mainloop()

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Sumeth Sathnindu
  • 323
  • 4
  • 15