【发布时间】:2019-01-29 20:27:41
【问题描述】:
前言:我目前已经尝试了大多数事情,在 stackoverflow 上关于这个问题的问题很少,我似乎无法理解它,所以我需要一些帮助
应用程序:这个程序的目的是为一个椭圆和一张图片制作动画,椭圆效果很好,但是图片很吃力,你可以删除图片或alien2 并且程序应该可以正常运行。
代码
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') Traceback(最近一次调用最后一次):
文件“”,第 1 行,在 runfile('/Users/Stian/.spyder-py3/animation_test.py', wdir='/Users/Stian/.spyder-py3')
文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 705 行,在运行文件中 execfile(文件名,命名空间)
文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 102 行,在 execfile exec(编译(f.read(),文件名,'exec'),命名空间)
文件“/Users/Stian/.spyder-py3/animation_test.py”,第 57 行,在 外星人()
文件“/Users/Stian/.spyder-py3/animation_test.py”,第 25 行,在 init 中 self.CardVar = ImageTk.PhotoImage(resCard,master=root)
文件“/anaconda3/lib/python3.6/site-packages/PIL/ImageTk.py”,第 124 行,在 init 中 self.__photo = tkinter.PhotoImage(**kw)
文件“/anaconda3/lib/python3.6/tkinter/init.py”,第 3542 行,在 init Image.init(self, 'photo', name, cnf, master, **kw)
文件“/anaconda3/lib/python3.6/tkinter/init.py”,第 3498 行,在 init self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: 无法调用“图像”命令:应用程序已被销毁
我知道代码并不漂亮,但它是一个测试环境,如上所述,一切都应该完美无瑕,但是当使用图像而不是椭圆形时它会中断,这应该不会有太大的区别。提前致谢!
问题已解决
原来在 PhotoImage() 中分配 master 时出现了问题,当我删除应用程序没有得到 TclError
新问题
我对代码所做的更改来自于此:
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 行,在 runfile('/Users/Stian/.spyder-py3/animation_test.py', wdir='/Users/Stian/.spyder-py3')
文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 705 行,在运行文件中 execfile(文件名,命名空间)
文件“/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”,第 102 行,在 execfile exec(编译(f.read(),文件名,'exec'),命名空间)
文件“/Users/Stian/.spyder-py3/animation_test.py”,第 56 行,在 外星人()
文件“/Users/Stian/.spyder-py3/animation_test.py”,第 27 行,在 init 中 self.alien2 = self.canvas.create_image(100,100,CardVar,anchor=CENTER)
文件“/anaconda3/lib/python3.6/tkinter/init.py”,第 2486 行,在 create_image return self._create('image', args, kw)
文件“/anaconda3/lib/python3.6/tkinter/init.py”,第 2477 行,在 _create *(args + self._options(cnf, kw))))
TclError: 未知选项“pyimage21”
【问题讨论】:
-
我们不需要看到图像形式的代码或错误。但是,如果您提供“bubble.png”图像会很好。
标签: python python-3.x tkinter compiler-errors spyder