类型错误:启动文件:文件路径应为字符串、字节或操作系统.路径,而不是_io.文本IOWrapper


import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
apps = []
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfile(initialdir="/", title="Select File",
filetypes=(("executables","*.exe"),("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)
canvas=tk.Canvas(root,height=700, width=700,bg="#263d42")
canvas.pack()

frame= tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8,relx = 0.1, rely=0.1)

openFile = tk.Button(root, text = "Open File", padx = 10, pady=5, fg="white", bg="#263d42",command=addApp)
openFile.pack()

runApps = tk.Button(root, text = "Run Apps", padx = 10, pady=5, fg="white", bg="#263d42", command=runApps)
runApps.pack()

root.mainloop()

函数filedialog.askopenfile()返回文件对象,而不是其名称。若要获取文件名,请使用filedialog.askopenfilename()

最新更新