Python:Tkinter TclError:无法调用"image"命令 [英] Python: Tkinter TclError: can't invoke "image" command

查看:90
本文介绍了Python:Tkinter TclError:无法调用"image"命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:我目前已经尝试了大多数事情,并且在stackoverflow上对此问题提出的问题很少,我似乎无法解决这个问题,所以即时通讯需要一些帮助

preface: I have currently tried most things and there are few qusestions asked on stackoverflow on this question and i cant seem to wrap my head around it so im going to need some help

应用程序 :该程序的目的是为椭圆和图片设置动画,该椭圆可以正常工作,但是图片很费力,您可以删除图片或 alien2 ,该程序应该可以正常运行.

application: the purpose of this program is to animate an oval and a picture, the oval works fine, but the picture is struggling, you could remove the image or alien2 and the program should run fine.

图像形式

from tkinter import *
import time
from PIL import Image,ImageFilter
from PIL import ImageTk

class alien(object):
     def __init__(self):


        self.root = Tk()
        self.canvas = Canvas(self.root, width=400, height = 400)
        self.canvas.pack()
        self.cardPath = Image.open('bubble.png')
        self.resCard = cardPath.resize((100,100))
        self.CardVar = ImageTk.PhotoImage(resCard,master=root)
        self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',fill='blue')
        self.alien2 = self.canvas.create_image(100,100,CardVar,anchor=CENTER)
        self.canvas.pack()
        self.root.after(0, self.animation)
        self.root.mainloop()


     def animation(self):
        track = 0
        while True:
            x = 5
            y = 0
            if track == 0:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, x, y)
                    self.canvas.move(self.alien2, x, y)
                    self.canvas.update()
               track = 1
               print("check")

            else:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, -x, y)
                    self.canvas.move(self.alien2, -x, y)
                    self.canvas.update()
               track = 0
            print(track)

alien()


错误:

图像

runfile('/Users/Stian/.spyder-py3/animation_test.py',wdir ='/Users/Stian/.spyder-py3')追溯(最近一次通话):

runfile('/Users/Stian/.spyder-py3/animation_test.py', wdir='/Users/Stian/.spyder-py3') Traceback (most recent call last):

文件",第1行,在运行文件('/Users/Stian/.spyder-py3/animation_test.py',wdir ='/Users/Stian/.spyder-py3')

File "", line 1, in runfile('/Users/Stian/.spyder-py3/animation_test.py', wdir='/Users/Stian/.spyder-py3')

runfile中的文件"/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",第705行execfile(文件名,命名空间)

File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile execfile(filename, namespace)

execfile中的文件"/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",第102行exec(compile(f.read(),文件名,'exec'),命名空间)

File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件"/Users/Stian/.spyder-py3/animation_test.py",第57行,在Alien()

File "/Users/Stian/.spyder-py3/animation_test.py", line 57, in alien()

"strong> init 中的文件"/Users/Stian/.spyder-py3/animation_test.py",第25行self.CardVar = ImageTk.PhotoImage(resCard,master = root)

File "/Users/Stian/.spyder-py3/animation_test.py", line 25, in init self.CardVar = ImageTk.PhotoImage(resCard,master=root)

init 中的文件"/anaconda3/lib/python3.6/site-packages/PIL/ImageTk.py",第124行self .__ photo = tkinter.PhotoImage(** kw)

File "/anaconda3/lib/python3.6/site-packages/PIL/ImageTk.py", line 124, in init self.__photo = tkinter.PhotoImage(**kw)

init 中的文件"/anaconda3/lib/python3.6/tkinter/init.py",第3542行图片.初始化(自己,照片",姓名,cnf,master,** kw)

File "/anaconda3/lib/python3.6/tkinter/init.py", line 3542, in init Image.init(self, 'photo', name, cnf, master, **kw)

init 中的文件"/anaconda3/lib/python3.6/tkinter/init.py",第3498行self.tk.call((''image','create',imgtype,name,)+选项)

File "/anaconda3/lib/python3.6/tkinter/init.py", line 3498, in init self.tk.call(('image', 'create', imgtype, name,) + options)

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

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


我知道代码不是很漂亮,但是它是一个测试环境,并且如上所述,所有内容都可以正常工作,但是当使用图像而不是椭圆形时,它会中断,这不会有太大的不同.预先感谢!


I know the code is not beautiful, but it's a test environment and as mentioned everything should work flawless, but it breaks when an image is used instead of an oval which should not make a big difference. Thanks in advance!

当我删除应用程序未得到 TclError <时,在 PhotoImage()中分配 master 时出现了问题/p>

新问题

turns out the problem turned up when assigning master inside PhotoImage(), when i removed that the application did not get the TclError

我对代码所做的更改来自于此:

the changes i made to the code were from this:

        self.cardPath = Image.open('bubble.png')
        self.resCard = cardPath.resize((100,100))
        self.CardVar = ImageTk.PhotoImage(resCard,master=root)

对此:

    self.cardPath = Image.open('bubble.png')
    self.resCard = cardPath.resize((100,100))
    self.CardVar = ImageTk.PhotoImage(resCard)


新错误

图像形式

文件",第1行,在运行文件('/Users/Stian/.spyder-py3/animation_test.py',wdir ='/Users/Stian/.spyder-py3')

File "", line 1, in runfile('/Users/Stian/.spyder-py3/animation_test.py', wdir='/Users/Stian/.spyder-py3')

runfile中的文件"/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",第705行execfile(文件名,命名空间)

File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile execfile(filename, namespace)

execfile中的文件"/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",第102行exec(compile(f.read(),文件名,'exec'),命名空间)

File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

文件"/Users/Stian/.spyder-py3/animation_test.py",第56行,在Alien()

File "/Users/Stian/.spyder-py3/animation_test.py", line 56, in alien()

"strong> init 中的文件"/Users/Stian/.spyder-py3/animation_test.py",第27行self.alien2 = self.canvas.create_image(100,100,CardVar,anchor = CENTER)

File "/Users/Stian/.spyder-py3/animation_test.py", line 27, in init self.alien2 = self.canvas.create_image(100,100,CardVar,anchor=CENTER)

在create_image中的文件"/anaconda3/lib/python3.6/tkinter/init.py",第2486行返回self._create('image',args,kw)

File "/anaconda3/lib/python3.6/tkinter/init.py", line 2486, in create_image return self._create('image', args, kw)

文件_create中的文件"/anaconda3/lib/python3.6/tkinter/init.py",行2477*(args + self._options(cnf,kw))))​​

File "/anaconda3/lib/python3.6/tkinter/init.py", line 2477, in _create *(args + self._options(cnf, kw))))

TclError:未知选项"pyimage21"

TclError: unknown option "pyimage21"

推荐答案

一段时间后,我编辑了代码并使之工作.这是完整的代码

After a while i edited the code and made it work. here is the full code

from tkinter import *
import time
import PIL
from PIL import Image,ImageFilter
from PIL import ImageTk

class alien(object):
    def __init__(self):

    self.root = Tk()
    self.canvas = Canvas(self.root, width=400, height = 400)
    self.canvas.pack()

    CardVar = ImageTk.PhotoImage(file='bubble.png')
    self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',fill='blue')
    self.alien2 = self.canvas.create_image((100,100),image=CardVar,anchor=CENTER)
    self.canvas.pack()
    self.root.after(0, self.animation)
    self.root.mainloop()


 def animation(self):
    track = 0
    while True:
        x = 5
        y = 0
        if track == 0:
           for i in range(0,51):
                time.sleep(0.025)
                self.canvas.move(self.alien1, x, y)
                self.canvas.move(self.alien2, x, y)
                self.canvas.update()
           track = 1
           print("check")

        else:
           for i in range(0,51):
                time.sleep(0.025)
                self.canvas.move(self.alien1, -x, y)
                self.canvas.move(self.alien2, -x, y)
                self.canvas.update()
           track = 0
        print(track)
alien()

问题

cardVar即使在同一文件夹中也始终无法找到正确的图像,因此我尝试执行cardVar.show(),并且显示了另一个位置较远的图像,因此似乎已将某些内容保存在其中我的记忆.所以我决定重启内核,一切顺利

这篇关于Python:Tkinter TclError:无法调用"image"命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆