473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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.

4 10325
boby
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
dwblas
626 Expert 512MB
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
boby
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
Can you solve your problem?
Jul 11 '19 #5

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

Similar topics

4
by: mtech1 | last post by:
Access 2000 I need to extract just the filename from a path. Ex: path is- c:\MyApp\Sub\MyApp.exe I need to capture just the file name regardless of the size of it's name. For the above...
4
by: Carsten Kraft | last post by:
Hello Newsgroup, I am searching for function which returns me filename from a string that has the file + filepath. ( string sFile = "C:\DIR\SUBDIR\TEST.XML" => TEST.XML) Is there a...
5
by: Kristoffer Persson | last post by:
It's probably very simple... I would like to know if there is a function similar to ExtractFileName() (I've used it in Delphi), which returns only the file name from the full path, e.g....
1
by: Daniel Cardoso | last post by:
I don't know what else to try - In my asp.net app, when the file name has Windows-1252 characters (like ã and ç), these characters appear, each one, as two strange characters in the file name label...
5
by: steve | last post by:
How can someone extract the file name and file path from a complete path? ex: c:\mydir\subdir\temp\myfile.txt will give: path = c:\mydir\subdir\temp filename = myfile.txt TIA
4
by: kevin | last post by:
Hi, I am trying to create a page so the user can browse the network, select a file and have that file name (text) inserted into our sql DB so I can build the hyperlink path later on. Here's what...
6
by: fatima.issawi | last post by:
Hello, Are brakets () allowed in a file path? eg. Passing: Y:\trak\pdf\98-00_2_123\tst.ps works but
1
by: Simon | last post by:
Dear reader, The function: Dim refCurr as Reference refCurr.Name
0
by: WillChapman | last post by:
Hello charitable, I have a repetitive task that requires the user to go to a command prompt, navigate to a folder (usually deep in the system), and perform a syntax scan on a file (with an in-house...
1
by: RamyaMuru | last post by:
its working on my pc, i want to do this on our website by clicking button, <script type="text/javascript" language="javascript"> function RunFile() { WshShell = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.