应用程序被破坏Tcl错误

应用程序被破坏Tcl错误

本文介绍了Python代码问题,应用程序被破坏Tcl错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Tkinter GUI,除了调用图像之外什么都不做——当然,我一直在努力寻找像样的 tkinter 文档.

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.

我的一行代码似乎无法按要求执行 - 我想调用字典中的所有值,并在调用下一个值之前为每个值分别打印和拉取相同名称的图像.我已经尝试过 dict.itervalues() 和 dict.values() 并且似乎无法完全弄清楚任何事情......

There is a line of my code which cannot seem to do as asked - I want to call up all the values in a dictionary and individually print and pull an image by the same name for each one before the next value is called up. I have tried dict.itervalues() and dict.values() and can't seem to figure anything out altogether...

无论如何,这是片段:

for key in ansDict.iterkeys(): #using the iterkeys function... kind of
    x=key

    root = tk.Tk() # root window created (is this in the right place?)
    root.title('C H E M I S T R Y   A B C\'s')

    frameAns=tk.Frame(root)
    frameAns.grid(row=0, column=0, sticky=tk.NW)

    for i in range(len(ansDict[x])):
        print '-->' + ansDict[x][i]

    for value in ansDict.itervalues(): #This is the most important part

        for i in range(len(value)): #pulls value list from dictionary named ansDict
            picRef1 = Image.open(value[i] + '.jpg') #calls image file by the same name using PIL
            photo1 = ImageTk.PhotoImage(picRef1, master=root)

            button1 = tk.Button(frameAns, compound=tk.TOP, image=photo1, text=str(value[i]) + '\nClose me!', bg='white') #pulls up button onto which the image is pasted
            button1.grid(sticky=tk.NW, padx=2, pady=2) #places button on grid
            button1.image=photo1

            root.mainloop()

最后,它拉出一两个图像,然后出现以下错误:

Finally, at the end, it pulls up one or two images and then I get the following error:

TclError:无法调用图像"命令:应用程序已被破坏

TclError: can't invoke "image" command: application has been destroyed

我不知道出了什么问题.我无法移动图像命令,不知何故我需要保存"它以免被破坏.我知道这里还有其他代码错误,但我认为如果我弄清楚我得到的 TclError,我可以直接设置其他所有内容.

and I can't figure out what is wrong. I can't move the image command, and somehow I need to "save" it so it isn't destroyed. I know there are other code errors here, but I think that if I figure out the TclError that I am getting that I can set everything else straight.

如果有更简单的方法可以做到这一切,请告诉!

If there is an easier way to do all this please do tell!

推荐答案

我已经四处寻找了一个很好的解决方案,但还没有找到合适的解决方案.查看 Tkinter.py 类,它的 Image del 值是:

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 destroyed
            pass

这意味着如果您想进行 BRUTAL hack,您可以按照 jtp 链接中的说明设置 PhotoImage.

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

这将阻止它尝试在 PhotoImage 删除中清理自己并阻止在 del 方法中调用异常.我真的不建议你这样做,除非你的背靠在墙上,而且你别无选择.

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

这篇关于Python代码问题,应用程序被破坏Tcl错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:41