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

问题描述 投票:0回答:1
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文件,然后采取适当的行动来打开给应用程序的applicationsfiles。

python macos subprocess
1个回答
0
投票

替换这一行

os.startfile(app)

由。

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

考虑一个特定的应用程序来使用 打开一个文件,你可以这样做。

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

其中 application 是您要打开的应用程序 app

© www.soinside.com 2019 - 2024. All rights reserved.