2014-04-19 84 views
0

我想製作一個簡單的工具,它將從一個地方複製/移動具有特定擴展名的文件到另一個地方。一切都很成功,但我試圖讓你可以輸入你想從中複製的目錄,然後選擇你要複製到的目錄。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() 
+0

使用'filedialog'爲此目的和也,瀏覽按鈕爲此事 – Gogo

+0

如果你想繼續這種方式,你想使用'binding'&'get()'獲得在條目 – Gogo

回答

1

我已經做了一些修改:

  • 使用class(更好的編碼技術)
  • 用於filedialog(用於獲取路徑)
  • 創建Browse按鈕
  • Modifi編輯你的代碼爲copyy & movee

在這裏,它是:

import shutil 
import os 
from tkinter import * 
import tkinter.filedialog as fdialog 

class MainClass(): 

    def __init__(self,master): 
     self.parent=master 
     self.gui() 

    def gui(self): 
     self.Source=StringVar() 
     self.Destination=StringVar() 

     MySource=Entry(myGUI, textvariable=self.Source).grid(row=9, column=2) 
     browse=Button(myGUI,text="Browse",command=lambda:self.Source.set(fdialog.askopenfilename(filetypes=[("JPEG File",'.jpg')]))).grid(row=9, column=3) 
     MyDestination=Entry(myGUI, textvariable=self.Destination).grid(row=10, column=2) 
     browse1=Button(myGUI,text="Browse",command=lambda:self.Destination.set(fdialog.askdirectory())).grid(row=10, column=3) 

     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=self.copyy).grid(row=3, column=0) 
     button2=Button(myGUI, text=" Move ", command=self.movee).grid(row=5, column=0) 


    def copyy(self): 
     source_file=self.Source.get() 
     if source_file.endswith(".jpg"): 
      shutil.copy(source_file, self.Destination.get()) 

    def movee(self): 
     source_file=self.Source.get() 
     if source_file.endswith(".jpg"): 
      shutil.move(source_file, self.Destination.get()) 

if __name__ == '__main__': 
    myGUI=Tk() 
    app=MainClass(myGUI) 
    myGUI.geometry("400x200+100+200") 
    myGUI.title('Copy dat') 
    myGUI.mainloop() 

如果您有任何疑問,我會很樂意幫助:)

+0

中輸入的文本非常美麗,謝謝!雖然移動按鈕不起作用,直到我剛剛複製複製按鈕的代碼,並將它的'shutil.copy'更改爲'shutil.move',不知道爲什麼。現在它複製而不是所有的.jpgs在目錄中,只是一個具體哪一點改變了工具的用途。除此之外 - 你已經幫助我在這件事情上取得進展,並向我展示了我必須做更多研究的東西:)所以,感謝一堆,我會繼續學習,嘗試新事物!再次感謝:) – NumeroSMG

+0

是的,我忘了改變那.. ..承諾 – Gogo

+0

如果你想獲得目錄中的所有文件,你想看看[os.walk()](https://docs.python.org/2 /library/os.html#os.walk) – Gogo

相關問題