By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

how to get a file name or file path after clicking a button and display the content i

P: 3
#from tkinter.scrolledtext import ScrolledText
from tkinter import *
import tkinter as tk
import tkinter.filedialog
from tkinter.filedialog import askopenfilename


Title_font = ("Comic Sans MS",20,"bold")
Sub_font =("Comic Sans MS",15,"bold")
Nor_font = ("Calibri",15)

class SampleApp(tk.Tk):

def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)

# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}
for F in (StartPage,FormPage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame

# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("StartPage")


def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()


class StartPage(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
lb1 = tk.Label(self, text="Filename", font= ('Plantagenet Cherokee',20,'bold'))
lb1.grid(row=3,column=8,sticky = "E")

entry_2 = Entry(self,font=('Arial',12))
entry_2.grid(row=3,column=9)

go_button = tk.Button(self, text="Browse",
command=self.selectfile)
gen_button = tk.Button(self, text="Generate",command =lambda: controller.show_frame("FormPage") )


go_button.grid(row=3,column=20,pady=20,padx=10)
gen_button.grid(row=4,column=9,pady=5)


def selectfile(self):

custName = StringVar(None)
fileName = askopenfilename(parent=self, title='Choose a file', initialdir='C:\\')
custName.set(fileName) #Populate the text field with the selected file

#entry_1 = Entry(self, width ='20', textvariable=custName)
#entry_1.grid(row=0,column= 1)






class FormPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Fill The Gap Questions:", font=("Plantagenet Cherokee",30,"bold"))
label.grid(row=4,column=2,pady=10)


# button = tk.Button(self, text="SUBMIT")
#button.grid(row=2,column=1,sticky=E,pady=5)
b2=tk.Button(self,text='BACK',command=lambda: controller.show_frame("StartPage"))
b2.grid(row=0,column=0,pady=5)




if __name__ == "__main__":

app = SampleApp()
app.geometry('400x400')
app.mainloop()
Apr 12 '17 #1

✓ answered by dwblas

The name is stored in the variable fileName via the following statement.
Expand|Select|Wrap|Line Numbers
  1. fileName = askopenfilename(parent=self, title='Choose a file', initialdir='C:\\') 
See http://effbot.org/tkinterbook/tkinter-file-dialogs.htm and https://gist.github.com/Yagisanatode...d4e3a871587ab1 which gets the name (name field), prints it and then opens and reads the file.

Share this Question
Share on Google+
4 Replies


P: 3
how to select a file name or file path after clicking a button and display that file name in entry box and also diaplaying the content of file in the text box?

plzz help.
im using tkinter for gui
thanku in advance
Apr 12 '17 #2

Expert 100+
P: 616
The name is stored in the variable fileName via the following statement.
Expand|Select|Wrap|Line Numbers
  1. fileName = askopenfilename(parent=self, title='Choose a file', initialdir='C:\\') 
See http://effbot.org/tkinterbook/tkinter-file-dialogs.htm and https://gist.github.com/Yagisanatode...d4e3a871587ab1 which gets the name (name field), prints it and then opens and reads the file.
Apr 13 '17 #3

P: 3
thnku for ur reply@dwblas...sir actually im new to this..and got stuck...bt i want to print the file name or file path when i select the file and the content shld b printed on the text box automatically...plzz help if u can...thnku
Apr 14 '17 #4

P: 1
Can you solve your problem?
Jul 11 '19 #5

Post your reply

Sign in to post your reply or Sign up for a free account.