使用PyCharm绘制龟(python)

问题描述 投票:4回答:5

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

import turtle

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

通过不工作我的意思是,没有窗口弹出,但我确实在输出说

Process finished with exit code 0

任何的想法

  1. 如果可以通过PyCharm完成
  2. 我在配置方面缺少什么

干杯

python pycharm turtle-graphics
5个回答
12
投票

我遇到了同样的问题。事实证明解决方案是在“乌龟”模块中。

你想结束

turtle.done()

要么

turtle.exitonclick()

请享用!


3
投票

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

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

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()

如果有人对如何不关闭画布有不同的想法,那就太棒了。

谢谢,


1
投票

正如Allan Anderson所说,我找到的最简单的方式(因为我经常不使用main):

turtle.exitonclick()

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


-1
投票

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

import turtle

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

wn.mainloop()

-1
投票

使用以下代码。您缺少使屏幕保持活动的功能,直到用户关闭为止。 exitonclick()方法有助于保持屏幕活跃。

import turtle

wn = turtle.Screen()    
alex = turtle.Turtle()
alex.forward(150)      
alex.left(90)     
alex.forward(75)    
wn.exitonclick()
© www.soinside.com 2019 - 2024. All rights reserved.