Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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

Post a Comment for "Python 3.2 Tkinter - Typed In Directory To Be Source For File Copying"