Welcome folks today in this blog post we will be opening external
files in python using a gui library called as tkinter. All the source code of the application will be given below.
Get Started
In order to get started you need to make sure python is installed on your system. And then you need to install tkinter
library using pip command
pip install tkinter
Method Used
We are using the system() method in the os module to open external programs
in python.
After installing it, now just make a app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Import Library from tkinter import * import os from tkinter.filedialog import askopenfilename # Create Object root = Tk() # Set geometry root.geometry('200x200') def open_file(): file = askopenfilename() os.system('"%s"' % file) Button(root, text ='Open', command = open_file).pack(side = TOP, pady = 10) # Execute Tkinter root.mainloop() |
Now if you execute your python script app.py
by running the following command
python app.py
you will see a button inside gui to open external files
Now you can see that if we click this button a new window will appear to select the files to open as shown below.
Now you can see that we have selected the image to open and it has automatically selected the photos app to open the image as shown below.