While working with GUI one may need to open files and read data from it or may require to write data in that particular file. One can achieve this with the help of
open() function (python built-in) but one may not be able to select any required file unless provides a path to that particular file in code.
With the help of GUI, you may not require to specify the path of any file but you can directly open a file and read it’s content.
In order to use askopenfile() function you may require to follow these steps:
-> import tkinter
-> from tkinter.filedialog import askopenfile ## Now you can use this function
-> file = askopeenfile(mode=’r’, filetypes=[(‘any name you want to display’, ‘extension of file type’)])
We have to specify the mode in which you want to open the file like in above snippet, this will open a file in reading mode.
Printed content of selected file –
Comparison of content of original file and printed content –
Note: In above code only
.py (python files) types files will be open. To open specified type of files, one has to mention it in the filetypes option along with it’s extension as done in above code.
- Python | Binding function in Tkinter
- Python | asksaveasfile() function in Tkinter
- Binding Function with double click with Tkinter ListBox
- Python GUI - tkinter
- Color game using Tkinter in Python
- Python | Message Encode-Decode using Tkinter
- Python - Tkinter askquestion Dialog
- Python | Simple registration form using Tkinter
- Python | Simple GUI calculator using Tkinter
- Python | Distance-time GUI calculator using Tkinter
- Python | Random Password Generator using Tkinter
- Python | Real time currency convertor using Tkinter
- Python | Creating a button in tkinter
- Python | Real time weather detection using Tkinter
- Python | Simple FLAMES game using Tkinter
- Python | Simple calculator using Tkinter
- Python | Add style to tkinter button
- Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
- Python Tkinter | Create LabelFrame and add widgets to it
- Python | geometry method in Tkinter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet