简体   繁体   English

在python的turtle模块中注册形状

[英]Registering a shape in python's turtle module

My problem: 我的问题:

I am attempting to register a shape with the python turtle module, however it is refusing to work. 我正在尝试向python turtle模块注册形状,但是它拒绝工作。 The gif in question is this and it is located in my downloads folder with the title tenor.gif. 有问题的gif文件是这个 ,它位于我的下载文件夹中,标题为tenor.gif。 I am running this code in the IDE, Canopy, in Python 3. 我正在用Python 3在IDE Canopy中运行此代码。

If I restart the kernel and then run the code, I consistently get the same output , verifying I have the right directory: 如果我重新启动内核,然后运行代码,我将始终获得相同的输出 ,并确认我具有正确的目录:

%run "C:/Users/AlexPC/Python Programs/Chess.py"
C:\Users\AlexPC\Downloads
C:\Users\AlexPC\Downloads

and the same error: 相同的错误:

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
C:\Users\AlexPC\Python Programs\Chess.py in <module>()
     37 print(os.getcwd())
     38 s.register_shape("tenor.gif")
---> 39 t.shape("tenor.gif")
     40 
     41 
C:\Users\AlexPC\AppData\Local\Enthought\Canopy\edm\envs\User\lib\turtle.py in shape(self, name)
   2775         if not name in self.screen.getshapes():
   2776             raise TurtleGraphicsError("There is no shape named %s" % name)
-> 2777         self.turtle._setshape(name)
   2778         self._update()
   2779 
C:\Users\AlexPC\AppData\Local\Enthought\Canopy\edm\envs\User\lib\turtle.py in _setshape(self, shapeIndex)
   2504             self._item = screen._createpoly()
   2505         elif self._type == "image":
-> 2506             self._item = screen._createimage(screen._shapes["blank"]._data)
   2507         elif self._type == "compound":
   2508             self._item = [screen._createpoly() for item in
C:\Users\AlexPC\AppData\Local\Enthought\Canopy\edm\envs\User\lib\turtle.py in _createimage(self, image)
    721         """Create and return image item on canvas.
    722         """
--> 723         return self.cv.create_image(0, 0, image=image)
    724 
    725     def _drawimage(self, item, pos, image):
<string> in create_image(self, *args, **kw)
C:\Users\AlexPC\AppData\Local\Enthought\Canopy\edm\envs\User\lib\tkinter\__init__.py in create_image(self, *args, **kw)
   2327     def create_image(self, *args, **kw):
   2328         """Create image item with coordinates x1,y1."""
-> 2329         return self._create('image', args, kw)
   2330     def create_line(self, *args, **kw):
   2331         """Create line with coordinates x1,y1,...,xn,yn."""
C:\Users\AlexPC\AppData\Local\Enthought\Canopy\edm\envs\User\lib\tkinter\__init__.py in _create(self, itemType, args, kw)
   2318         return self.tk.getint(self.tk.call(
   2319             self._w, 'create', itemType,
-> 2320             *(args + self._options(cnf, kw))))
   2321     def create_arc(self, *args, **kw):
   2322         """Create arc shaped region with coordinates x1,y1,x2,y2."""
TclError: image "pyimage1" doesn't exist 

My code: 我的代码:

from turtle import Turtle, Screen
import os


t = Turtle("square")
t.shapesize(4,4)
t.hideturtle()
t.pu()  
t.goto(-280,-280)

s = Screen()
s.clearscreen()
s.tracer(False)
s.screensize(800,800)

#register the piece shapes
print(os.getcwd())
os.chdir("C:\\Users\\AlexPC\\Downloads")
print(os.getcwd())
s.register_shape("tenor.gif")
t.shape("tenor.gif")

A couple of issues. 有几个问题。 First, your image link led to an animated GIF -- there's nothing in the turtle documentation about animated GIF support. 首先,您的图片链接导致了GIF动画-在turtle文档中没有有关GIF动画支持的内容。 You'll likely just get a static GIF image at best, nothing at worst. 充其量您可能只会得到静态的GIF图像,最坏的情况下则不会。

Second, your example code has unrelated and unecessary stuff. 其次,您的示例代码包含无关且不必要的内容。 Let's simplify and focus on the problem: 让我们简化并专注于该问题:

from turtle import Turtle, Screen
import os

screen = Screen()
screen.screensize(800, 800)
os.chdir(""C:\\Users\\AlexPC\\Downloads"")
screen.register_shape("tenor.gif")

turtle = Turtle("tenor.gif")
turtle.penup()  
turtle.goto(-280, -280)

screen.mainloop()

This worked for me on a Unix system, putting the image in a "Downloads" directory under my current directory and using a relative os.chdir() to relocate there -- I don't know if this is a Windows-specific problem. 这在Unix系统上对我os.chdir() ,将映像放在当前目录下的“下载”目录中,并使用相对的os.chdir()移至该位置-我不知道这是否是Windows特定的问题。 Give the above a try to see if it works any better for you. 尝试上面的方法,看看它是否对您更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM