0

I wanted to create a GUI button using Python Tkinter Module to open up a specific app that has already been installed on the computer, e.g. 'calculator'.

I have found a similar question on StackOverflow but its not quite what I want as it only open the file directory and not the app itself.

Thanks.

  • Can you elaborate on this *what I want as it only open the file directory and not the app itself* –  Jul 19 '21 at 11:25
  • Is [this](https://stackoverflow.com/questions/35807817/how-do-i-run-an-exe-from-python-using-a-tkinter-button) what you are trying to do ? – Alexandre Marcq Jul 19 '21 at 11:25
  • @Sujay I am trying to create a GUI button using tkinter to open up the calculator app when clicked on it. The post that I found seems to only open up the file explorer with the path of the app but not the app itself. – BartSimpson Jul 19 '21 at 11:44
  • @AlexandreMarcq Kind of like that. I'll try to apply it to my code now. – BartSimpson Jul 19 '21 at 11:46

2 Answers2

2

Another way is to use the subprocess module to create a new process like this:

import tkinter as tk
from subprocess import Popen

root = tk.Tk()

def open_calc():
    Popen("calc.exe")

button = tk.Button(root, text="Open Calculator", command=open_calc)
button.pack()

root.mainloop()

This method is more intuitive to me than using webbrowser. The subprocess module was designed to create new processes

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Thank you, this will save me much in trying to find the directory location. By the way, is there a process in subprocess module to allow images to be easily called in, instead of pasting the whole link to the directory? – BartSimpson Jul 19 '21 at 12:13
  • @BartSimpson Are the images in the same directory as the python file? – TheLizzard Jul 19 '21 at 12:14
  • Kind of, I put it inside a folder named 'images' within the same directory as my python code file. – BartSimpson Jul 19 '21 at 12:17
  • Then you can simply open it using - `PhotoImage(file='images/pics.png')` – PCM Jul 19 '21 at 12:20
  • @PCM That isn't going to run with the default windows image viewer and you will need more code that than. – TheLizzard Jul 19 '21 at 12:21
  • @BartSimpson For images, (you already found it but) use `os.startfile("images/test.png", "open")`. That means you will be using `os` instead of `subprocess` – TheLizzard Jul 19 '21 at 12:22
  • I meant it for displaying on to Tkinter window and not to open the pictures. BTW @BartSimpson , what is the extra '@' – PCM Jul 19 '21 at 12:23
  • 1
    @PCM Seems I forgot to write add a name after '@'. Don't think I can edit anymore too. – BartSimpson Jul 20 '21 at 16:48
0

First of all, find the path location of the app you want. File path for calculator in my system is C:\Windows\System32\calc.exe

So use webbrowser to open it -

import tkinter as tk
import webbrowser

root = tk.Tk()

def open_calc():
    webbrowser.open_new('C:\Windows\System32\calc.exe ')

B1 = tk.Button(root,text='hi',command=open_calc)
B1.pack()

root.mainloop()
PCM
  • 2,881
  • 2
  • 8
  • 30
  • Thank you so much, I'll now apply it to my code and try to create a button with the image of the calculator on it. – BartSimpson Jul 19 '21 at 11:47
  • BTW, for any other apps, probably you should search google if you cannot find the file paths – PCM Jul 19 '21 at 11:48
  • Thank you, this is going to help a lot for my project. – BartSimpson Jul 19 '21 at 11:49
  • ok, what? I guess webbrowser's open functionality is the same as `os.startfile()` it just looks a bit odd, so an alternative would be to `import os` and then instead of `webbrowser.open_new('C:\Windows\System32\calc.exe ')` use `os.startfile('C:\Windows\System32\calc.exe ')` or since using windows might as well do this: `os.system('calc')` instead of all of that path and stuff, tho actually maybe not use `os.system` – Matiiss Jul 19 '21 at 11:55