我想製作一個簡單的工具,它將從一個地方複製/移動具有特定擴展名的文件到另一個地方。一切都很成功,但我試圖讓你可以輸入你想從中複製的目錄,然後選擇你要複製到的目錄。Python 3.2 Tkinter - 在目錄中輸入的文件複製源
myGUI=Tk()
myGUI.geometry("400x200+100+200")
myGUI.title('Copy dat')
Source=StringVar()
Destination=StringVar()
MySource=Entry(myGUI, textvariable=Source).grid(row=9, column=2)
MyDestination=Entry(myGUI, textvariable=Destination).grid(row=10, column=2)
def copyy():
source = os.listdir("Source")
destination = "Destination"
for files in source:
if files.endswith(".jpg"):
shutil.copy(files, destination)
button1=Button(myGUI, text=" Copy ", command=copyy).grid(row=3, column=0)
但如果我點擊我的按鈕,該錯誤消息說,Windows無法找到指定的目錄/來源或類似的東西。所以我明白source = os.listdir("Source")
是問題所在。我猜destination = "Destination"
也是incorect。 如果我在代碼中放置整個路徑,複製按鈕效果不錯,但我希望可以由工具的用戶在窗口中寫入路徑。請幫忙。
編輯:全碼如果需要的話:
import shutil
import os
from tkinter import *
myGUI=Tk()
myGUI.geometry("400x200+100+200")
myGUI.title('Copy dat')
Source=StringVar()
Destination=StringVar()
MySource=Entry(myGUI, textvariable=Source).grid(row=9, column=2)
MyDestination=Entry(myGUI, textvariable=Destination).grid(row=10, column=2)
def copyy():
source = os.listdir('Source')
destination = "Destination"
for files in source:
if files.endswith(".jpg"):
shutil.copy(files, destination)
def movee():
source = os.listdir("C:/Users/PC/Desktop/python testing/")
destination = "C:/Users/PC/Desktop/python testing/destination"
for files in source:
if files.endswith(".jpg"):
shutil.move(files, destination)
label1=Label(myGUI, text='Welcome to the copy utility', fg='Blue').grid(row=0,column=2)
label2=Label(myGUI, text='Ultimate JPG mover', fg='Black').grid(row=1,column=0)
label3=Label(myGUI, text='(Thing\'s actually pretty useless)', fg='Black').grid(row=2,column=0)
button1=Button(myGUI, text=" Copy ", command=copyy).grid(row=3, column=0)
button2=Button(myGUI, text=" Move ", command=movee).grid(row=5, column=0)
myGUI.mainloop()
使用'filedialog'爲此目的和也,瀏覽按鈕爲此事 – Gogo
如果你想繼續這種方式,你想使用'binding'&'get()'獲得在條目 – Gogo