保存海龟绘图 [英] Save turtle drawing

查看:556
本文介绍了保存海龟绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 python 程序,它绘制一个正方形,我希望用户能够在用户按下 q 时保存绘图.它所做的只是将图像添加到 ide 文件目录中.如果有帮助,我会使用 Replit 作为我的 ide.它不会将其直接保存到用户文件中.相关代码如下:

I am making a python program where it draws a square and I want the user to be able to save the drawing when the user presses q. All it does is add the image to the ide file directory. If it helps, I use Replit as my ide. It does not save it directly to the users files. Here is the relevant code:

import turtle
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
def save():
 ts = turtle.getscreen()
 ts.getcanvas().postscript(file="Square.png")
turtle.onkeypress(save, key="q")

提前致谢!

如果是操作系统特定的 Windows、Chrome 操作系统和 Mac.

If it is os specific Windows, Chrome Os, and Mac.

推荐答案

看,您应该来这里是为了获得有关如何自己解决问题的提示.根据我的提示,您应该能够解决这个问题.这不是代码编写服务.另外,你的问题陈述不够清楚.您说将其直接保存到用户文件".这对你来说代表着什么?文档目录?他的主目录?你想让他选择吗?而且你真的想每次都使用相同的文件名吗?

Look, you're supposed to be coming here to get hints on how to solve your problems on your own. Given my hints, you should have been able to solve this problem. This is not a code-writing service. Also, your problem statement is not clear enough. You said "save it directly to the users files". What does that mean to you? The Documents directory? His home directory? Do you want him to choose? And do you really want to use the same file name every time?

import os
...
def save():
   filename = os.environ["HOME"] + "/Square.png"
   ts = turtle.getscreen()
   ts.getcanvas().postscript(file=filename)
turtle.onkeypress(save, key="q")

这篇关于保存海龟绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆