我想使用Python Tkinter模块创建一个GUI按钮,以打开计算机上已经安装的特定应用程序,例如"计算器"。
我在StackOverflow上发现了一个类似的问题,但这不是我想要的,因为它只打开文件目录,而不是应用程序本身。
谢谢。
另一种方法是使用subprocess
模块创建这样的新流程:
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()
这种方法对我来说比使用webbrowser
更直观。subprocess
模块设计用于创建新流程
首先,找到您想要的应用程序的路径位置。我的系统中计算器的文件路径是C:WindowsSystem32calc.exe
所以用webbrowser
打开它-
import tkinter as tk
import webbrowser
root = tk.Tk()
def open_calc():
webbrowser.open_new('C:WindowsSystem32calc.exe ')
B1 = tk.Button(root,text='hi',command=open_calc)
B1.pack()
root.mainloop()