在 Mac 上使用 os.startfile() 并实现解决方案时遇到问题


import tkinter as tk
from tkinter import filedialog, Text
import os, sys , subprocess

# holds the whole app structure
root = tk.Tk()
#shows app in interface
apps = []
if os.path.isfile('save.txt'):
with open('save.txt','r') as f:
tempApps = f.read()
tempApps = tempApps.split(',')
apps = [x for x in tempApps if x.strip()]
print(tempApps)
# defining functions to add to the app interface
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("apps","*.*"),("all files","*.*")))
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app,bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)


#creating our interface size and look
canvas = tk.Canvas(root,height = 700, width = 600, bg = "#263D42")
#showing the interface
canvas.pack()
#creating an inner white frame to hold the information
frame = tk.Frame(root,bg='white')
frame.place(relwidth=.8,relheight=.8,relx = 0.1,rely = 0.1)

#creating buttons for apps
openFile = tk.Button(root, text="Open File", padx=10,
pady=5,fg='black',bg='#263D42',command = addApp)
#shows app on the GUI interface
openFile.pack()
#creating buttons for apps
runApps = tk.Button(root, text="Run Apps", padx=10,
pady=5,fg='black',bg='#263D42',command= runApps)
#creating buttons for apps
runApps.pack()
for app in apps:
label = tk.Label(frame, text=app)
label.pack()

root.mainloop()
with open('save.txt', 'w') as f:
f.append(apps + ',')

此应用程序将用于在早上首次启动笔记本电脑时加快打开应用程序的过程,截至目前,此应用程序仅适用于Windows,我希望使其在Mac OS上兼容。 问题出在 os.startfile(( 命令中,我想实现一个 if 语句来读取它是 Mac 还是 Windows 文件,然后采取适当的打开提供给应用程序的应用程序/文件的操作。

替换此行

os.startfile(app)

由:

subprocess.call(["open", app])

要考虑使用特定应用程序打开文件,您可以执行以下操作:

subprocess.call(f"open -a {application} {app}")

其中application是您要打开的应用app依据

试试这种结构:

def runApps():
for app in apps:
subprocess.call(["open", app])

相关内容

最新更新