How to open External Programs using Tkinter?
Prerequisite: Tkinter, os
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, we will learn how to open an external program using Tkinter. Here we will use the system() method in the os module.
Approach:
- First, we will import the required library
- Then we create an object, which will ask for a file that you want to open
- To open that file we will use the system() method from the os Module.
Syntax:
os.system(‘”%s”‘ % File Path)
Below is the Implementation:
Python3
from tkinter import *
import os
from tkinter.filedialog import askopenfilename
root = Tk()
root.geometry( '200x200' )
def open_file():
file = askopenfilename()
os.system( '"%s"' % file )
Button(root, text = 'Open' ,
command = open_file).pack(side = TOP,
pady = 10 )
root.mainloop()
|
Output:
Opening application with their name
Prerequisite: Tkinter, AppOpener
The AppOpener is the python library that helps in opening any application without knowing it’s an absolute path. The library works by making use of App name and App Id.
Approach:
- First, we will import the required library.
- Then we create a text area, which takes the input of the application name that we have to open.
- To open that specific application we will use open() function of AppOpener module.
Syntax:
AppOpener.open(app_name)
Below is the Implementation:
Python3
import tkinter as tk
import AppOpener
root = tk.Tk()
root.geometry( "500x500" )
text_area = tk.Text(root, height = 10 , width = 30 , font = ("", 20 ))
text_area.pack()
text_area.focus()
button = tk.Button(root, text = "Open App" , font = ("", 20 ))
button.pack()
label = tk.Label(root, text = " ", font=(" ", 15 ))
label.pack()
label.config(text = "JUST ENTER NAME OF APPLICATION TO OPEN" )
def open_app(event):
app_name = text_area.get( "1.0" , "end" )
AppOpener. open (app_name)
label.config(text = str ( "Looking for " + app_name))
text_area.delete( "1.0" , "end" )
button.bind( "<Button-1>" , open_app)
root.mainloop()
|
Output:
Output of the Implementation of code
Last Updated :
23 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...