Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to open External Programs using Tkinter?
Sometimes, while creating an application, we need to interact with external programs and applications. In order to interact with the system's applications and programs, we have to use os Module in python.
In this article, we will see how we can interact with external programs and open files using the OS module in Python.
First, we will define a function that will open the chosen file using the filedialog library in Python. Then, we will print the path and open the file using the os module.
Example
Let's create a Tkinter application that allows users to browse and open external programs ?
# Import the required Libraries
from tkinter import *
from tkinter import filedialog
import os
# Create an instance of tkinter frame
win = Tk()
# Set the geometry for the window or frame
win.geometry("600x400")
# Define a function to open the application
def app():
file = filedialog.askopenfilename()
text.config(text=file)
# Open the program
os.system('"%s"' % file)
# Create a button
Button(win, text='Click to Open a Program', font=('Poppins bold', 10),
command=app).pack(pady=20)
# Create a Label after button event
text = Label(win, text="", font=('Poppins bold', 10))
text.pack(pady=20)
# Keep running the window or frame
win.mainloop()
Output
Running the above code will produce the following window as the output −
Now, click the button and it will open the "My Documents" folder from where you can open a program.
How It Works
The application works in the following steps ?
-
filedialog.askopenfilename()opens a file browser dialog - The selected file path is displayed in the label
-
os.system()executes the selected file as an external program - The
"%s"formatting handles file paths with spaces properly
Alternative Approach Using subprocess
For better control and error handling, you can use the subprocess module instead of os.system() ?
from tkinter import *
from tkinter import filedialog
import subprocess
win = Tk()
win.geometry("600x400")
def open_program():
file_path = filedialog.askopenfilename()
if file_path:
text.config(text=file_path)
try:
subprocess.run([file_path], check=True)
except subprocess.CalledProcessError:
text.config(text="Error: Could not open the selected file")
Button(win, text='Select and Open Program', font=('Arial', 12),
command=open_program).pack(pady=20)
text = Label(win, text="No file selected", font=('Arial', 10))
text.pack(pady=20)
win.mainloop()
Key Points
-
os.system()is simple but has security risks with untrusted input -
subprocessprovides better error handling and security - Always handle file path edge cases (spaces, special characters)
- Consider adding file type filters to
askopenfilename()
Conclusion
Using Tkinter with the os or subprocess modules allows you to create applications that can launch external programs. The subprocess approach is recommended for better error handling and security.
