3

I am trying to make a simple GUI using Tkinter in python2, in which I need to make an entry box and a button besides that. The button browses the file and shows the filepath in the entrybox. How can I do that.

I used the tkFileDialog.askopenfilename that allows to browse the path but how can I make the gui to show that path in an entry box.

I tried it as follows:

import tkinter as tk
import tkFileDialog

root=tk.Tk()
def browsefunc():
    filename =tkFileDialog.askopenfilename(filetypes=(("tiff files","*.tiff"),("All files","*.*")))

ent1=tk.Entry(frame,font=40)
ent1.grid(row=2,column=2)

b1=tk.Button(frame,text="DEM",font=40,command=browsefunc)
b1.grid(row=2,column=4)

root.mainloop()

Attached a screenshot of what I needs. Attached is the screenshot for desired GUI

Community
  • 1
  • 1
lsr729
  • 752
  • 2
  • 11
  • 25

2 Answers2

4

Are you really sure, that you are using python2? Because you wrote tkinter with a lowercase t and not with an uppercase T or did you just write it wrong?.

Anyway, you can easily insert a little text (in your case a path) into your Entry-widget by using the insert method of the Entry-widget. In your case it would be:

import Tkinter as tk
import tkFileDialog

root=tk.Tk()    

ent1=tk.Entry(root,font=40)
ent1.grid(row=2,column=2)

def browsefunc():
    filename =tkFileDialog.askopenfilename(filetypes=(("tiff files","*.tiff"),("All files","*.*")))
    ent1.insert(tk.END, filename) # add this

b1=tk.Button(root,text="DEM",font=40,command=browsefunc)
b1.grid(row=2,column=4)

root.mainloop()

The tk.END parameter gives the last index of the entry-string back.
If you already wrote something into the Entry-Widget like that:

This is my path:

and you add your path, than it will looks like that:

This is my path:/usr/bin/... 

As you can see it adds the string in the end of the "entry-string".
The other option would be 0 for the index than your path will be in the beginning of the entry-widget:

/usr/bin...HI

I'm sorry if my english is horrible! Feel free to edit it!

TornaxO7
  • 1,150
  • 9
  • 24
  • Thanks it worked, But I want the entry button to take new filepath after pressing the button again. It just adds the paths when pressed multiple times. How to get that? – lsr729 Aug 07 '19 at 13:42
  • just write this line over `ent1.insert(tk.END, filename`: ```python ent1.delete(0, tk.END) ``` It will clear the entry-widget from beginning (0) till the end (tk.END) of the entry-string. – TornaxO7 Aug 07 '19 at 13:59
  • 2
    Technically `tk.END` doesn't return the last index of the entry string. If you execute `print(tk.END)` it will just output the string `end`. The underlying tk framework knows that you mean the end of the string. – scotty3785 Aug 07 '19 at 15:14
  • what should I type in `filetype` if I want to take user input of directory's path? – lsr729 Aug 07 '19 at 19:00
  • I don't really understand what you mean. Doesn't the program put the path into the entry or what exactly do you mean? – TornaxO7 Aug 07 '19 at 19:11
  • When I specify `filetypes=(("tiff files","*.tiff"),("All files","*.*"))`, it takes files either `.tif` extension or any file. But I want it to select a directory path but not a file. how can I make it do that? – lsr729 Aug 08 '19 at 03:57
  • 1
    Probably https://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – TornaxO7 Aug 08 '19 at 06:34
  • and when I type`filetypes=(("tiff files","*.tif"))` only, it gives error, why? – lsr729 Aug 08 '19 at 08:51
  • I just got the answer at https://stackoverflow.com/questions/52183821/only-one-file-type-to-be-accepted thanks for your time – lsr729 Aug 08 '19 at 08:53
0

May be try this code it might work

   from tkinter import *
   from tkinter.filedialog import askopenfilename
   root=Tk()

   ent1=Entry(root,font=40)
   ent1.grid(row=2,column=2)

   def browsefunc():
      filename = askopenfilename(filetypes=(("jpg file", "*.jpg"), ("png file 
      ",'*.png'), ("All files", "*.*"),))
      ent1.insert(END, filename) # add this

      b1=Button(root,text="DEM",font=40,command=browsefunc)
      b1.grid(row=2,column=4)

   root.mainloop()
soldour
  • 11
  • 4