os.rename()使用用户输入变量?

时间:2013-11-05 15:27:51

标签: python python-3.x tkinter

我正在尝试重命名文件夹,该文件夹是通过请求用户输入和在其中创建的子文件夹创建的,以重命名父目录(asset_name)我正在使用filedialog.askdirectory()但我一直遇到错误

TypeError: Can't convert 'StringVar' object to str implicitly

我已经阅读了os.rename()上可以找到的所有文档,它总是引用(“路径”,“路径”)而不是变量,我尝试向后工作以减轻错误但是没有有用,我会在这里发布整个错误:

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()

1 个答案:

答案 0 :(得分:2)

您的错误与os.rename()几乎没有关系;引发异常是因为您尝试将tkinter GUI对象与字符串连接起来:

newname = ("completed- "+ currentproject)

currentproject此处不是字符串,它是StringVar()对象:

currentproject = StringVar()

.get()上致电StringVar()以获取当前值:

newname = "completed-" + currentproject.get()

请注意,这里根本不需要括号。

您还需要在self_Browse()中正确设置变量:

def sel_Browse():
    fldr = filedialog.askdirectory()
    currentproject.set(fldr)