1 回答
TA贡献1798条经验 获得超3个赞
该open功能仅适用于文件!
打开方式: os.system(filepath)
但是如果你想打开一个文件夹,你可以使用os.startfile[Windows]!
示例代码:
from tkinter import *
from tkinter import filedialog
import os
root = Tk()
root.geometry("400x400")
def selectDirectory():
global dirname
global folder_path
dirname = filedialog.askdirectory(parent=root,initialdir="/",title='Please select a
directory')
folder_path.set(dirname)
print(dirname)
folder_path=StringVar()
def fileopen():
os.system('%s.csv' % folder_path)
def folderopen():
os.startfile(dirname)
# Creating buttons
selectFolder = Button(root, text = "Select directory", command = selectDirectory)
selectFolder.grid(row=0,column=0)
openfile = Button(root, text = "Open file", command = fileopen)
openfile.grid(row=1, column=0)
openfolder = Button(root, text = "Open folder", command = folderopen)
openfolder.grid(row=2, column=0)
root.mainloop()
添加回答
举报