[Tutor] Tkinter problem (.create_image)

Rob Dowell rob at customdatasoft.com
Thu Oct 27 01:11:35 CEST 2005


I am having a problem with Tkinter. The goal is a program that will copy 
images from one folder to another then delete them from the source dir 
after it is verified that they made it to the target dir. I have the 
base functionality figured out (comments on that are welcome of course). 
Now I want to have a gui that shows a small thumbnail of the image as it 
is copied. Right now everything works fine except that the gui only 
shows the last picture that is copied. Here is the code:

from Tkinter import *
import PIL.Image as image
import PIL.ImageTk as imagetk
import ConfigParser
import os
import time
import shutil


   

class MyApp:
    def __init__(self, parent):
        self.myParent = parent
        self.myContainer1 = Frame(parent)
        self.myContainer1.pack()

        self.button1 = Button(self.myContainer1)
        self.button1.configure(text="Copy Pics")
        self.button1.pack()
        self.button1.bind("<Button-1>", self.button1Click)

        self.tv1 = StringVar()
        self.tv1.set("Ready to copy.")
       
        self.label1 = Label(self.myContainer1)
        self.label1.configure(textvariable = self.tv1)
        self.label1.pack()

        self.canvas1 = Canvas(self.myContainer1)
        self.canvas1.configure(width=200, height=200, bg='yellow')
        self.canvas1.pack()

    def button1Click(self, event):
        files = {}
        print "Here"
        #Import config information from the ini file
        configfile = "./GetPics.ini"
        config = ConfigParser.ConfigParser()
        config.read(configfile)
        sourcedirloc = config.get("GetPics", "sourcedir")
        targetdirloc = config.get("GetPics", "targetdir")
        deleteold = config.get("GetPics", "deleteold")
        enddict = {'1':'st',
                   '2':'nd',
                   '3':'rd',
                   '4':'th',
                   '5':'th',
                   '6':'th',
                   '7':'th',
                   '8':'th',
                   '9':'th',
                   '0':'th'}
        #OPen the source dir and get a list of the files making the new 
name and path as you go
        sourcefiles = os.listdir(sourcedirloc)
        files = {}
        for file in sourcefiles:
            filepath = sourcedirloc + "/" + file
            if files.has_key(filepath):
                #The file is duplicated?
                pass
            else:
                files[filepath] = {}
                files[filepath]['originalname'] = file
                files[filepath]['modtuple'] = 
time.localtime(os.stat(filepath).st_mtime)
                files[filepath]['size'] = os.stat(filepath).st_size
                files[filepath]['monthdir'] = targetdirloc + "/" + 
time.strftime('%Y-%m', time.localtime(os.stat(filepath).st_mtime))
                files[filepath]['daydir'] = files[filepath]['monthdir'] 
+ "/" + time.strftime('%d', time.localtime(os.stat(filepath).st_mtime)) 
+ enddict[time.strftime('%d', 
time.localtime(os.stat(filepath).st_mtime))[len(time.strftime('%d', 
time.localtime(os.stat(filepath).st_mtime)))-1]]
                files[filepath]['newname'] = files[filepath]['daydir'] + 
"/" + time.strftime('%Y-%m-%d-%H-%M-%S', 
time.localtime(os.stat(filepath).st_mtime)) + os.path.splitext(file)[1]
                #Now check to see if a file with this name already 
exists and increment the name if it does
                n = 1
                while os.path.isfile(files[filepath]['newname']):
                    files[filepath]['newname'] = 
os.path.splitext(files[filepath]['newname'])[0] + "-" + str(n) + 
os.path.splitext(files[filepath]['newname'])[1]
                    n += 1                   
        #Copy all of the file to the target dir
        for file in files.keys():
            #Check for the dir and create it if needed:
            if not os.path.isdir(files[file]['monthdir']):
                self.tv1.set("Creating (monthdir) " + 
files[file]['monthdir'])
                os.mkdir(files[file]['monthdir'])
            if not os.path.isdir(files[file]['daydir']):
                self.tv1.set("Creating (daydir) " + files[file]['daydir'])
                os.mkdir(files[file]['daydir'])
            #copy the file
            self.tv1.set("Copying " + file + " to " + 
files[file]['newname'])

        #  DISPLAY THE THUMBNAIL
            self.DisplayImage(file)

            shutil.copy2(file, files[file]['newname'])

        #Go back and remove the sorce files checking that the target 
file exists first
        for file in files.keys():
            if os.stat(files[file]['newname']).st_size == 
files[file]['size'] and deleteold.lower() == "true":
                print deleteold.lower()
                self.tv1.set("Deleting " + file)
                os.remove(file)
            else:
                self.tv1.set("There was an error with " + file)
        self.tv1.set("Copy done. " + str(len(files)) + " files were 
copied.")
       
    def DisplayImage(self, file):
        im = image.open(file)
        origx, origy = im.size
        if origx > origy:
            ratio = 100./origx
        else:
            ratio = 100./origy
        size = (int(ratio*origx), int(ratio*origy))
        out = im.resize(size)
        self.pic1 = imagetk.PhotoImage(out)
        self.canvas1.create_image(0, 0, image = self.pic1, anchor = NW) - 1

# run it ...
root = Tk()
myapp = MyApp(root)
root.mainloop()



Thanks in advance for any help.



More information about the Tutor mailing list