简体   繁体   English

无法在 jupyter notebook 上运行海龟(海龟图形窗口没有响应)

[英]Can't run turtle on jupyter notebook (turtle graphic window not responding)

First Question第一个问题

I was doing an example from ThinkPython 2e Chapter 4 Case Study: Interface Design, when I stumbled upon turtle module.当我偶然发现海龟模块时,我正在做一个来自 ThinkPython 2e 第 4 章案例研究:界面设计的示例。

I use jupyter notebook as an IDE to do exercise.我使用 jupyter notebook 作为 IDE 来做练习。 Here's the code这是代码

import turtle
bob = turtle.Turtle()

When I ran the code above, the code is exected but Python Turtle Graphic failed to run with 'Not Responding' status当我运行上面的代码时,代码被执行,但 Python Turtle Graphic 无法以“无响应”状态运行

The same goes for this, although when I ran the following, the kernal is busy for a while before stating an error这也是如此,尽管当我运行以下命令时,内核在声明错误之前忙了一段时间

import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()

What caused this error?是什么导致了这个错误?

Second Question第二个问题

Then I tried the next code which is然后我尝试了下一个代码

bob.fd(100)

and it just froze so I have to interrupt the code, here's the error它只是冻结了,所以我不得不中断代码,这是错误

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-a3ce06f254ac> in <module>
      2 bob = turtle.Turtle()
      3 print(bob)
----> 4 turtle.mainloop()

~\Anaconda3\lib\turtle.py in mainloop()

~\Anaconda3\lib\turtle.py in mainloop(self)
    811 
    812         """
--> 813         TK.mainloop()
    814 
    815     def textinput(self, title, prompt):

~\Anaconda3\lib\tkinter\__init__.py in mainloop(n)
    558 def mainloop(n=0):
    559     """Run the main loop of Tcl."""
--> 560     _default_root.tk.mainloop(n)
    561 
    562 getint = int

KeyboardInterrupt: 

What I have tried我试过的

I've tried to refer to this thread Turtle does not run more than once in jupyter notebook我试图参考这个线程Turtle does not run more than once in jupyter notebook
which eventually leads me back to here最终把我带回到这里

https://medium.com/@jiyuanli93/how-to-make-python-turtle-works-in-jupyter-notebook-6c506b9a973f https://medium.com/@jiyuanli93/how-to-make-python-turtle-works-in-jupyter-notebook-6c506b9a973f

or here或在这里

https://github.com/gkvoelkl/ipython-turtle-widget https://github.com/gkvoelkl/ipython-turtle-widget

a little information : when I run this on my Anaconda prompt一点信息:当我在我的 Anaconda 提示符上运行它时

$ jupyter nbextension install --py --symlink --sys-prefix ipyturtle
$ jupyter nbextension enable --py --sys-prefix ipyturtle

It generated some problems, but ran perfectly (it's said validating: ok ) when I ran them as admin它产生了一些问题,但是当我以管理员身份运行它们时运行完美(据说validating: ok

any help would be appreciated, I've been stuck with this stuff for 2.5 hours任何帮助将不胜感激,我已经被这些东西困住了 2.5 小时

I had the same problem as you.我和你有同样的问题。

This worked for me.这对我有用。

Put the following code into three Jupyter Notebook cells:将以下代码放入三个 Jupyter Notebook 单元格中:

Cell 1单元格 1

import turtle
bob = turtle.Turtle()
print(bob)

Cell 2单元格 2

for i in range(4):
    bob.fd(100)
    bob.lt(90)

Cell 3单元格 3

turtle.mainloop()

And run it one by one.并一一运行。

I am a noob, like you, but what I think is happening is that the code turtle.mainloop() terminates the object bob, or something like that.我和你一样是个turtle.mainloop() ,但我认为正在发生的是代码turtle.mainloop()终止了对象 bob,或者类似的东西。 What you should do is execute all the movements like bob.fd(100) before executing turtle.mainloop() .你应该做的是在执行turtle.mainloop()之前执行所有的动作,比如bob.fd(100) turtle.mainloop() Does it make sense?是否有意义?

ipyturtle seems to not work with JupyterLab or VS Code. ipyturtle 似乎不适用于 JupyterLab 或 VS Code。 Try ipyturtle3.试试ipyturtle3。 With python3.6+:使用python3.6+:

python -m pip install ipyturtle3

Try the examples listed in this repo: https://github.com/williamnavaraj/ipyturtle3试试这个 repo 中列出的例子: https ://github.com/williamnavaraj/ipyturtle3

https://pypi.org/project/ipyturtle3/ https://pypi.org/project/ipyturtle3/

I found this to work in JupyterLab and VSCode我发现这适用于 JupyterLab 和 VSCode

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

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