所以我试图通过Tkinter创建一个程序,用户可以通过文件系统进行操作。我有其他不起作用的按钮的单独代码,但重命名按钮让我很难过。我怎么能在这段代码中关闭filedialog?
这是我的代码。
from tkinter import *
import sys,glob,time,os
from tkinter import filedialog
def dir2():
global direc
direc=filedialog.askdirectory(initialdir="/",title="Choose directory")
dire1.config(text=direc)
print(direc)
mainloop()
def cancelr():
rrf.destroy()
root.deiconify()
def rnf():
name1=os.path.basename(rpath.name)
filee=None
for filee in glob.glob(name1):
if os.path.isdir(filee)==False:
os.rename(name1,nfn.get())
print("done")
elif file == None:
print("nothing")
def rf():
global fn
global rrf
global rpath
global name1
nfn=StringVar()
nfn.set(" ")
rpath=filedialog.askopenfile(initialdir=direc,title="Rename a file")
os.chdir(direc)
rrf=Toplevel(root)
root.iconify()
rrf.geometry('350x100+500+200')
body=Frame(rrf)
body.pack()
Label(body,text="Enter new file name: ",font=("Arial",15)).pack()
nfname=Entry(body,textvariable=nfn, width=50 ).pack()
body2=Frame(rrf)
body2.pack()
Button(body2,text="Done", font= ("Arial",10),command=rnf).grid(row=0,column=0,padx=5,pady=4)
Button(body2,text="Cancel",font=("Arial",10),command=cancelr).grid(row=0,column=1,padx=5,pady=4)
mainloop()
def creation():
os.chdir(direc)
fname=fn.get()
file=open(fname,"w")
file.close
done=messagebox.askyesno("File created!","Continue creating file?")
if done==True:
return
else:
crf.destroy()
root.deiconify()
def cancelc():
crf.destroy()
root.deiconify()
def df():
dfname=filedialog.askopenfilename(initialdir=direc,title="Choose a file")
done=messagebox.askyesno("Confirm","Delete file?")
if done==True:
os.remove(dfname)
messagebox.showinfo("Success!","File deleted")
else:
return
def cf():
global fn
global crf
fn=StringVar()
fn.set(" ")
crf=Toplevel(root)
root.iconify()
crf.geometry('350x100+500+200')
body=Frame(crf)
body.pack()
Label(body,text="Enter file name: ",font=("Arial",15)).pack()
fname=Entry(body,textvariable=fn, width=50, ).pack()
body2=Frame(crf)
body2.pack()
Button(body2,text="Done", font= ("Arial",10),command=creation).grid(row=0,column=0,padx=5,pady=4)
Button(body2,text="Cancel",font=("Arial",10),command=cancelc).grid(row=0,column=1,padx=5,pady=4)
print(fname)
mainloop()
def qui():
exit()
def butttons():
create=Button(body, text="Create file",command=cf,font=("Arial",12))
create.grid(row=0,column=1,padx=10,pady=4)
de=Button(body,text="Delete file",font=("Arial",12),command=df)
de.grid(row=0,column=2,padx=5,pady=4)
body2=Frame(root)
body2.pack()
re=Button(body2,text="Rename",font=("Arial",12),command=rf)
re.grid(row=0,column=0,padx=6,pady=4)
ld=Button(body2,text="List directory",font=("Arial",12))
ld.grid(row=0,column=1,padx=3,pady=4)
body3=Frame(root)
body3.pack()
tr=Button(body3,text="Traverse",font=("Arial",12))
tr.grid(row=0,column=0,pady=4,padx=12)
se=Button(body3,text="Search file",font=("Arial",12))
se.grid(row=0,column=1,pady=4,padx=9)
body4=Frame(root)
body4.pack()
qu=Button(body4,text="Quit",command=qui,font=("Arial",12))
qu.pack()
def dirr():
global dire1
global brow1
global direc
direc=StringVar()
direc=filedialog.askdirectory(initialdir="/",title="Choose directory")
print(direc)
brow.destroy()
gs.destroy()
desc.destroy()
dire1=Label(header, text=direc, font=("Arial",10),
relief=SUNKEN,anchor=W, justify=LEFT,width=30)
dire1.grid(row=0,column=1)
brow1=Button(header,text="Browse", command=dir2,font=("Arial",10))
brow1.grid(row=0,column=2)
butttons()
mainloop()
root=Tk()
root.title("File Systems")
root.geometry('300x200+100+50')
header=Frame(root)
header.pack()
gs=Label(header,text='''
Getting Started''',font=("Arial",17))
gs.pack(anchor="center")
body=Frame(root)
body.pack()
desc=Label(body,text='''
Choose a directory:''',font=("Arial",12))
desc.pack()
brow=Button(body, text=("Browse"),font=("Arial",10),command=dirr)
brow.pack()
mainloop()