[英]OS.rename() not working with tkinter
Folks I'm trying to make a tool which gets user path and file name in one entry and new file name with path in another entry, My aim is to use os.rename(oldname, newname)
to rename the given file, but its throwing me some error. 伙计们,我正在尝试制作一个在一个条目中获取用户路径和文件名,在另一个条目中获取用户名和文件名的工具,我的目的是使用os.rename(oldname, newname)
重命名给定文件,但是丢给我一些错误。
My code 我的密码
from tkinter import *
import os
def Rename_Function(*args):
os.rename(oldname2,newname)
oldname.set(oldname)#"Renamed Successfully !!! ")
root = Tk()
root.title("MyPython App")
root.geometry("250x250+100+50")
oldname = StringVar()
oldname2= StringVar()
newname= StringVar()
Title1 = Label(root,text="FileName (with path):")
Title1.grid(row=0, column=0)
Oldfilename = Entry(root, textvariable=oldname2)
Oldfilename.grid(row=0, column=1)
Title2 = Label(root, text="New Filename:")
Title2.grid(row=1, column=0)
Newfilename = Entry(root, textvariable=newname)
Newfilename.grid(row=1, column=1)
RenameButton = Button(root, text="RENAME MY FILE", command=Rename_Function)
RenameButton.grid(row=3,columnspan=2, sticky="NWES")
FinalOutput = Label(textvariable=oldname)
FinalOutput.grid(row=4, columnspan=2, sticky = "NWES")
root.mainloop()
I'm getting the above error when I click the button, Can someone guide me how to make it work. 单击按钮时出现上述错误,有人可以指导我如何使其正常工作。
I have a doubt that os.rename()
function should be accessed in some other way since its another module's function. 我怀疑os.rename()
函数是否应该以其他方式访问,因为它是另一个模块的函数。 Since I'm learner I don't have any clue how to use them effeciently. 因为我是学习者,所以我不知道如何有效地使用它们。 Please guide me and explain me so that I would understand this concept better. 请指导我并向我解释,以便我更好地理解这个概念。
To expand upon what @SuperSaiyan said in the comment(s). 扩展@SuperSaiyan在评论中所说的内容。
You are using a StringVar
, which has the method .get()
available to it. 您正在使用StringVar
,该方法具有可用的.get()
方法。 When you pass the variable which is set to this stringvar you are just passing the reference to this object. 当您传递设置为此stringvar的变量时,您只是将引用传递给该对象。 You need to actually use the .get()
method to get the string. 您实际上需要使用.get()
方法来获取字符串。
eg - oldname2.get()
例如oldname2.get()
Also, for selecting the path you could just use a filedialog, and use os.path.splitext
to get the base path + entry in the renaming widget to use as the second parameter with os.rename
另外,对于选择路径,您可以只使用filedialog,然后使用os.path.splitext
获取重命名小部件中的基本路径+条目,以用作os.rename
的第二个参数
You are using StringVar
, whereas rename
needs Strings. 您正在使用StringVar
,而rename
需要字符串。 Use oldname.get()
: 使用oldname.get()
:
import tkinter as tk
import os
def rename(oldname, oldname2, newname):
os.rename(oldname2.get(),newname.get())
oldname.set("Renamed Successfully !!! ")
def main():
root = tk.Tk()
root.title("MyPython App")
root.geometry("250x250+100+50")
oldname = tk.StringVar()
oldname2= tk.StringVar()
newname= tk.StringVar()
tk.Label(root, text="FileName (with path):").grid(row=0, column=0)
tk.Entry(root, textvariable=oldname2).grid(row=0, column=1)
tk.Label(root, text="New Filename:").grid(row=1, column=0)
tk.Entry(root, textvariable=newname).grid(row=1, column=1)
tk.Button(root, text="RENAME MY FILE", command=lambda: rename(oldname, oldname2, newname)).grid(row=3,columnspan=2, sticky="NWES")
tk.Label(textvariable=oldname).grid(row=4, columnspan=2, sticky = "NWES")
root.mainloop()
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.