我正在尝试重命名一个文件夹,该文件夹是通过请求用户输入和在其中创建的子文件夹来创建的,以重命名我正在使用的父目录(asset_name),filedialog.askdirectory()
但我一直遇到错误
TypeError: Can't convert 'StringVar' object to str implicitly
我已经阅读了我能找到的所有文档os.rename()
,它总是指 ("path","path") 而不是变量,我尝试向后工作以减轻错误但无济于事,我将发布整个这里的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "<module1>", line 59, in fldrRename
TypeError: Can't convert 'StringVar' object to str implicitly
这是代码:
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import tix
from tkinter.constants import *
from tkinter import filedialog
import os, sys
import itertools
import getpass
root = Tk()
root.title("WRT Project Manager")
path = ("P://Projects_2013//")
mainframe = ttk.Frame(root, padding="12 12 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
prjtnm= StringVar()
Dirlist= StringVar()
in2 = StringVar()
in3 = StringVar()
assetVar = StringVar()
dirlst = StringVar()
fldr = StringVar()
currentproject = StringVar()
#get a list of all the directories
def dirlist():
from tkinter.filedialog import askdirectory
dirlist = path
if dirlist:
path.set(dirlist)
# create project folder and sub directories
def genAsset():
asset_name = assetVar.get()
os.chdir("P:\\Projects_2013")
dirs = [[asset_name],
["fldr1", "fldr2", "fldr3", "fldr4", "fldr5", "fldr6"]]
for item in itertools.product(*dirs):
os.makedirs(os.path.join(*item))
#choose an existing project directory
def sel_Browse():
fldr = filedialog.askdirectory()
currentproject = fldr
prjtnm.delete(0, END)
prjtnm.insert(0, fldr)
#rename a folder 'completed-'
def fldrRename():
newname = ("completed- "+ currentproject)
os.chdir("P:\\Projects_2013")
os.rename(currentproject,newname)
ttk.Label(mainframe, text="User =").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe,text=getpass.getuser()).grid(column=2, row=1, sticky=W)
ttk.Label(mainframe, text="Project directory path is P:/Projects_2013/").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="Enter NEW project name:").grid(column=2, row=2, sticky=E, pady=15)
ttk.Entry(mainframe, textvariable= assetVar, width=50).grid(column=3, row=2, sticky=(W,E))
ttk.Button(mainframe, text="Create Folders", command=genAsset).grid(column=5, row=2, sticky=E, padx=10)
ttk.Label (mainframe, text='Select an existing project:').grid(column=2,row=3, sticky=E)
ttk.Button(mainframe, text="Browse", command=sel_Browse).grid(row=3,column=5, sticky=E, padx=10)
prjtnm = Entry(mainframe, width=50)
prjtnm.grid(row=3,column=3)
ttk.Button(mainframe, text="Close", command=fldrRename).grid(row=4,column=3, padx=10)
ttk.Label(mainframe, text="Select Date").grid(column=1, row=6, sticky=W)
in3_entry = ttk.Entry(mainframe, width=20, textvariable=in3)
in3_entry.grid(column=2, row=6, sticky=(W, E))
root.mainloop()