
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Opening and reading a file with askopenfilename in Tkinter?
When a user wants to open a file from a directory, the preferred way to do this is to display a popup where the user selects a file to Open. Like most tools and widgets, Tkinter provides us a way to open a dialog for opening a file, reading a file, saving a file. All these functionalities are part of filedialog Module in Python. Just like other widgets, filedialog needs to be imported explicitly in the notebook. There are certain other modules that contain the filedialog such as, askdirectory, askopenfilename, askopenfile, askopenfilenames, asksaveasfilename, etc.
Example
In this example, we will define a function to open and read the file using askopenfilename.
We will define an application which contains a button to open a file and will pack the content of the file in a Label widget. In order to read the file content, we will use the read() method along with the filename.
#Import tkinter library from tkinter import * from tkinter import ttk from tkinter import filedialog #Create an instance of tkinter frame or window win= Tk() win.geometry("750x150") #Define a function to Opening the specific file using filedialog def open_files(): path= filedialog.askopenfilename(title="Select a file", filetypes=(("text files","*.txt"), ("all files","*.*"))) file= open(path,'r') txt= file.read() label.config(text=txt, font=('Courier 13 bold')) file.close() button.config(state=DISABLED) win.geometry("750x450") #Create an Empty Label to Read the content of the File label= Label(win,text="", font=('Courier 13 bold')) label.pack() #Create a button for opening files button=ttk.Button(win, text="Open",command=open_files) button.pack(pady=30) win.mainloop()
Output
Running the above code will display a window which contains a button which when clicked, will open a new window to load and read the file content.
Click the "Open" button to open the file(text, "*") in the window.
- Related Articles
- Reading a Text file in java
- Reading and writing binary file in C/C++
- Reading a JSON file using Swift
- Reading and Writing CSV File using Python
- Programmatically opening URLs in a web browser in Python (tkinter)
- Reading/Writing a MS Word file in PHP
- Different ways of Reading a file in C#
- How to improve file reading performance in Python with MMAP function?
- Reading a text file into an Array in Node.js
- How to open an Excel file with PHPExcel for both reading and writing?
- Reading UTF8 data from a file using Java
- File opening modes(r versus r+) in C++
- What does opening a file actually do on Linux?
- SAP Crystal Report file is not opening in VS2015
- How do we specify the buffer size when opening a file in Python?
