Skip to content Skip to sidebar Skip to footer

Python Code Problem, Application Has Been Destroyed Tcl Error

I am making a Tkinter GUI to do nothing except call images - and of course, I have struggled to find decent tkinter documentation all along. There is a line of my code which cannot

Solution 1:

I have looked around for a good solution to this but have yet to find the proper solution. Looking at the Tkinter.py class it looks like the Image del value is:

def__del__(self):
    if self.name:
        try:
            self.tk.call('image', 'delete', self.name)
        except TclError:
            # May happen if the root was destroyedpass

This means if you wanted to do a BRUTAL hack you could setup a PhotoImage as described in jtp's link.

photo = tk.PhotoImage(file="C:/myimage.gif")
widget["image"] = photo
widget.image = photo

Then you could just before the program exited do the following hack:

photo.name = None

This would prevent it from trying to clean itself up in the PhotoImage delete and prevent the exception from being called in the del method. I do not really recommend you do this unless your back is up against the wall, and you have no alternative.

I will continue to look into this and if I find a better solution will edit this post with a better one (hopefully someone will give the correct solution before then).

Solution 2:

Here is one possibility, although it is structured differently than your example. It stacks the four 100 pixel square images on top of one another. I believe you need to keep a separate reference to each Image object, so I tucked them away in the images dictionary.

from Tkinter import *
import os
from PIL import Image, ImageTk

image_names = { '1':'one','2':'two','3':'three','4':'four' }
images = {}

root = Tk()
root.title("HELLO")
frm = Frame(root)

for v in image_names.itervalues():
   images[v] = {}
   images[v]['i']  = Image.open("%s%s.jpg" % (os.path.dirname(__file__), v))
   images[v]['pi'] = ImageTk.PhotoImage(images[v]['i'])
   images[v]['b']  = Button(frm, image=images[v]['pi'])
   images[v]['b'].pack()

frm.pack()

mainloop()

Here is a good link discussing the PhotoImage class.

http://effbot.org/tkinterbook/photoimage.htm

Solution 3:

It seems that you did not get the idea of Event-driven programming. You should create whole GUI once, fill it with widgets, setup the events and then enter infinite loop. The GUI should call callback functions based on your event to function binding. So those parts of your program should definitely be called just once: root = tk.Tk(), root.mainloop().

Edit: Added Event-driven programming "idea example".

from Tkinter import *

master = Tk()

defcallback():
    print"click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

Post a Comment for "Python Code Problem, Application Has Been Destroyed Tcl Error"