我正在使用tkinter创建一个python程序,该程序将根据字符串创建文件夹名称,并根据选中的复选框创建子文件夹。
即。如果选中“banana”复选框,它将在名为folder的字符串中创建一个banana文件夹。这就是我需要做的事情:
到目前为止,这是我的代码。我知道可能有更好的方法来存储所有复选框但我不知道该怎么做:
import tkinter as tk
from tkinter import *
from tkinter.filedialog import askdirectory
import os, sys
def browse():
dir = askdirectory()
if dir:
path.set(dir)
def genAsset():
assetPath=path.get()
assetName=asset.get()
expFold=export.get()
bakFold=bake.get()
sceFold=scene.get()
texFold=texture.get()
scuFold=sculpt.get()
refFold=reference.get()
renFold=render.get()
engFold=engine.get()
os.makedirs(assetPath+"/"+assetName+"/")
os.makedirs(assetPath+"/"+assetName+"/"+expFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+bakFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+sceFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+texFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+scuFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+refFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+renFold+"/")
os.makedirs(assetPath+"/"+assetName+"/"+engFold+"/")
#create the window and title
window=Tk()
window.title("v1.0")
#define variables and images
path=StringVar()
asset=StringVar()
VMR=PhotoImage(file="VMR.gif")
selectFolders=StringVar()
export=StringVar()
bake=StringVar()
scene=StringVar()
texture=StringVar()
sculpt=StringVar()
reference=StringVar()
render=StringVar()
engine=StringVar()
#define checkbuttons of folders to generate and store textvaiable
expButt=Checkbutton(window, text="export", textvariable=export).pack()
bakButt=Checkbutton(window, text="bake").pack()
sceButt=Checkbutton(window, text="scene").pack()
texButt=Checkbutton(window, text="texture").pack()
scuButt=Checkbutton(window, text="sculpt").pack()
refButt=Checkbutton(window, text="reference").pack()
renButt=Checkbutton(window, text="renders").pack()
engButt=Checkbutton(window, text="engine").pack()
#display the image file
hdrImg=Label(window, image=VMR)
hdrImg.photo=VMR
#select path and define asset name, store strings
lDirec=Label(window, text="Folder Directory:").pack()
ePath=Entry(window, textvariable=path, width=50).pack()
bBrowse=Button(window, text="Browse Path", command=browse).pack()
lAsset=Label(window, text="Asset Name:").pack()
eAsset=Entry(window, textvariable=asset, width=50).pack()
bCreate=Button(window, text="Create Asset", command=genAsset).pack()
window.mainloop()
答案 0 :(得分:0)
您可以创建两个列表:一个用于子目录的名称,另一个用于存储IntVar
以跟踪是否检查每个Checkbutton
。因此,您可以在genAsset
函数中压缩两个列表,如果相应的IntVar
的值不为零,则创建子目录。
通过这种方式,您还可以减少回调函数中的大量代码重复,并简化小部件的创建。
import tkinter as tk
from tkinter.filedialog import askdirectory
import os, sys
window = tk.Tk()
def browse():
directory = askdirectory()
if directory:
path.set(directory)
def genAsset():
asset_path = path.get()
asset_name = asset.get()
for var, folder in zip(variables, folders):
folder_path = os.path.join(asset_path, asset_name, folder)
if var.get() and not os.path.exists(folder_path):
os.makedirs(folder_path)
path = tk.StringVar()
asset = tk.StringVar()
folders = ["export", "bake", "scene", "texture", "sculpt", "reference", "renders", "engine"]
variables = []
for folder in folders:
var = tk.IntVar()
tk.Checkbutton(window, text=folder, variable=var).pack()
variables.append(var)
dir_label = tk.Label(window, text="Folder Directory:")
path_entry = tk.Entry(window, textvariable=path, width=50)
browse_button = tk.Button(window, text="Browse Path", command=browse)
asset_label = tk.Label(window, text="Asset Name:")
asset_entry = tk.Entry(window, textvariable=asset, width=50)
create_button = tk.Button(window, text="Create Asset", command=genAsset)
dir_label.pack()
path_entry.pack()
browse_button.pack()
asset_label.pack()
asset_entry.pack()
create_button.pack()
window.mainloop()
PS:请记住,pack()
会返回None
,因此您不应存储调用此函数的结果,而是先创建小部件,然后再调用pack()
。