3 回答
TA贡献1801条经验 获得超16个赞
这是你想要的吗?
import os
from tkinter.filedialog import askdirectory
import tkinter.messagebox
import tkinter as tk
# Dir select
def selectPath():
path_ = askdirectory()
path.set(path_)
def create_file():
print("folder_name: ", folder.get())
print("path_name: ", path.get())
dirs = os.path.join(path.get(), folder.get())
if not os.path.exists(dirs):
os.makedirs(dirs)
tkinter.messagebox.showinfo('Tips:','Folder name created successfully!')
else:
tkinter.messagebox.showerror('Tips','The folder name exists, please change it')
root = tk.Tk()
root.title('Create folder')
root.geometry('400x380')
path = tk.StringVar() # Receiving user's file_path selection
folder = tk.StringVar() # Receiving user's folder_name selection
tk.Label(root,text = "Target path:").place(x=50, y= 250)
tk.Entry(root, textvariable = path).place(x=110, y= 250)
tk.Button(root, text = "Path select: ", command = selectPath).place(x=265, y= 250)
tk.Label(root,text = "Folder name:").place(x=50, y= 300)
tk.Entry(root,textvariable = folder).place(x=110, y= 300)
tk.Button(root, text = "Submit: ", command = create_file).place(x=265, y= 300)
root.mainloop()
path和folder变量是选择的路径和用户创建的文件夹名称。
它在我的电脑上看起来像这样:
TA贡献1909条经验 获得超7个赞
您可以通过放置一个input称为文本小部件的小部件来做到这一点。在输入文件夹名称并使用选择路径后,filedialog您可以创建一个创建目录的函数。
def create_dir()
name = file_name.get() # took from the input
path = os.path.join(dir, name) # dir is a directory taken from the filedialog
TA贡献1869条经验 获得超4个赞
from datetime import datetime
import os
from tkinter import filedialog
datestring = datetime.today().strftime('%Y-%m-%d') #Formats the date according to year,month,day.
#You can check out datetime for more info
default_folder = 'Adrain-{}'.format(datestring)
if os.path.exists(default_folder) == True: #Only creates a new folder when non-existing
pass
else:
os.mkdir(default_folder)
file_directory = filedialog.askdirectory(title = 'Select the target folder for saving
records', initialdir = default_folder)
添加回答
举报