Python 即使用笔,如何阻止乌龟画画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15602192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:31:49  来源:igfitidea点击:

How to stop turtle from drawing even with pen up?

pythonpython-3.xturtle-graphics

提问by derpyherp

I am using the turtle module in python. the problem is that whenever I have the turtle move i will draw even if the pen is up. for example if I run this program:

我在 python 中使用乌龟模块。问题是,每当我让乌龟移动时,即使笔在上面,我也会画画。例如,如果我运行这个程序:

import turtle

turtle.penup
turtle.goto(0,50)

the turtle will still draw a line when it moves to (0,50) why is this and how can it be prevented?

海龟在移动到 (0,50) 时仍然会画一条线,这是为什么,如何防止?

回答by svk

It looks like you're not actually calling turtle.penup. Try this:

看起来您实际上并没有调用turtle.penup。尝试这个:

import turtle

turtle.penup()
turtle.goto(0,50)

回答by Ikke

You have a typo, you aren't calling the penup method:

你有一个错字,你没有调用 penup 方法:

import turtle

turtle.penup() #This needs to be a method call
turtle.goto(0,50)

回答by user5390283

no it should be something like this:

不,它应该是这样的:

turtle.up()         # This a method call
turtle.goto(0,50)   # Part of the method call

回答by Naval

import turtle

进口龟

turtle.up() turtle.goto(0,50) turtle.down()

turtle.up() turtle.goto(0,50) turtle.down()

if you don't put the pen down it will keep on drawing in invisible condition.

如果你不放下笔,它会在隐形状态下继续绘图。

回答by ng10

you called penup without (). with

你在没有 () 的情况下调用了 penup。和

turtle.penup()

this will work.

这会起作用。

Others here said that, but implicitly. trying to ensure it is clear where the typo is.

这里的其他人说,但含蓄。试图确保清楚错字在哪里。

回答by wjmccann

This question is super old and definitely has already been answered, but I'll leave this explanation here for future people

这个问题太老了,肯定已经有人回答了,但我会把这个解释留在这里给未来的人

"penup" is a method in Python, aka function in other languages. This means that when you want to use it you have it include some parenthesis just so that your code knows what is supposed to be happening

“penup”是 Python 中的一种方法,也就是其他语言中的函数。这意味着当您想使用它时,您可以将它包含一些括号,以便您的代码知道应该发生什么

import turtle

turtle.penup()
turtle.goto(0,50)

When you don't include the parenthesis, the code thinks you are talking about a variable, and looks for one called "penup", but there is no variable of that name, so Python throws its hands up and crashes

当你不包括括号时,代码认为你在谈论一个变量,并寻找一个名为“penup”的变量,但没有这个名字的变量,所以 Python 举手并崩溃

回答by Kirshan Murali

You should probably try,

你应该试试,

turtle.penup()