Search code examples
python-3.xpathtkintercopymove

Python 3.2 Tkinter - typed in directory to be source for file copying


I am trying to make a simple tool which will copy/move files with certain extension from one place to another. Everything is working out pretty much fine, but I am trying to make it possible to type in the directory where you want to copy from, and choose the directory where you want to copy to.

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)

But if I click on my button, the error message says that windows can't find directory named /Source or something like that. So I understand source = os.listdir("Source")is the problem. And I guess destination = "Destination" is also incorect. If I put the whole path in the code the copy button works well, but I want it to be possible to write the path in the window by the user of the tool. Please help.

edit: The whole code if needed:

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

Solution

  • I have made some modifications:

    • Used class (better coding technique)
    • Used filedialog(for getting the path)
    • Created Browse button
    • Modified your code for copyy & movee.

    Here,it is:

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

    If you have any doubts,I would be happy to help :)