使用 PyCharm 绘制乌龟(python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26737366/
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-19 00:57:14  来源:igfitidea点击:

Drawing with turtle(python) using PyCharm

pythonpycharmturtle-graphics

提问by DanyC

I'm running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesn't seem to work

我正在运行最新的 PyCharm Pro 版本并尝试从一个草稿文件运行以下代码,但它似乎不起作用

import turtle

wn = turtle.Screen() 
alex = turtle.Turtle()
alex.forward(150)  
alex.left(90) 
alex.forward(75)

By not working i mean, no window is popping out however i do see in the ouput saying

不工作我的意思是,没有窗口弹出但是我在输出中看到说

Process finished with exit code 0

Any idea

任何的想法

  1. If it can be done via PyCharm
  2. What am i missing in terms of configuration
  1. 如果可以通过 PyCharm 完成
  2. 我在配置方面缺少什么

Cheers

干杯

采纳答案by DanyC

I managed to find out a way of doing it by using the below code.

我设法通过使用以下代码找到了一种方法。

The only downside is that it does close the canvas once it finished.

唯一的缺点是它在完成后会关闭画布。

def main():
    wn = turtle.Screen()  # creates a graphics window
    alex = turtle.Turtle()  # create a turtle named alex
    alex.forward(150)  # tell alex to move forward by 150 units
    alex.left(90)  # turn by 90 degrees
    alex.forward(75)  # complete the second side of a rectangle

if __name__ == "__main__":
    main()

If anyone has a different idea on how to not close the canvas that will be awesome.

如果有人对如何不关闭画布有不同的想法,那将会很棒。

Thanks,

谢谢,

Dani

达尼

回答by Allan Anderson

I ran into the same problem. Turns out the solution is in the "turtle" module.

我遇到了同样的问题。原来解决方案在“海龟”模块中。

You want to end with

你想以

turtle.done()

乌龟.完成()

or

或者

turtle.exitonclick()

海龟.exitonclick()

Enjoy!

享受!

回答by phil3000

As Allan Anderson says, easiest way I found (as I don't use main that often):

正如艾伦安德森所说,我发现的最简单的方法(因为我不经常使用 main ):

turtle.exitonclick()

As the last line of code forces the Graphics Window to stay open until it is clicked.

由于最后一行代码强制图形窗口保持打开状态,直到它被单击。

回答by Mike

By "no window is popping out" means that the program is executing and then directly closing. To fix it you have to loop the program as follows:

“没有窗口弹出”意味着程序正在执行然后直接关闭。要修复它,您必须按如下方式循环程序:

import turtle

wn = turtle.Screen() 
alex = turtle.Turtle()
alex.forward(150)  
alex.left(90) 
alex.forward(75)

wn.mainloop()

回答by vivek manchikatla

Use the below code. You are missing the function which keep the screen alive until it is closed by the user. exitonclick()Method helps to keep screen alive.

使用下面的代码。您缺少使屏幕保持活动状态直到用户关闭它的功能。exitonclick()方法有助于使屏幕保持活动状态。

import turtle

wn = turtle.Screen()    
alex = turtle.Turtle()
alex.forward(150)      
alex.left(90)     
alex.forward(75)    
wn.exitonclick()

回答by Udai Verma

def main():
    import turtle
    wn = turtle.Screen()  # creates a graphics window
    alex = turtle.Turtle()  # create a turtle named alex
    alex.forward(150)  # tell alex to move forward by 150 units
    alex.left(90)  # turn by 90 degrees
    alex.forward(75)  # complete the second side of a rectangle


if __name__ == "__main__":
    main()

    input("Press RETURN to close.  ")

The last line will hold display till RETURN key is not pressed.

最后一行将保持显示直到没有按下 RETURN 键。