{"id":985368,"date":"2024-12-27T07:36:03","date_gmt":"2024-12-26T23:36:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985368.html"},"modified":"2024-12-27T07:36:05","modified_gmt":"2024-12-26T23:36:05","slug":"python%e4%b8%adtuetle%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985368.html","title":{"rendered":"python\u4e2dtuetle\u5982\u4f55\u4fdd\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212551\/74592fa1-69d7-4211-8859-f12dfaccac72.webp\" alt=\"python\u4e2dtuetle\u5982\u4f55\u4fdd\u5b58\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5c06\u56fe\u5f62\u4fdd\u5b58\u3002<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>screenshots<\/code>\u5de5\u5177\u3001<code>canvas<\/code>\u65b9\u6cd5\u3001\u6216\u5c06\u56fe\u5f62\u8f6c\u5316\u4e3a<code>PostScript<\/code>\u6587\u4ef6\u6765\u4fdd\u5b58Turtle\u56fe\u5f62<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u4e3a\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3aPostScript\u6587\u4ef6\uff0c\u56e0\u4e3a\u8fd9\u79cd\u683c\u5f0f\u53ef\u4ee5\u4fdd\u6301\u56fe\u5f62\u7684\u77e2\u91cf\u7279\u6027\uff0c\u65b9\u4fbf\u540e\u671f\u7f16\u8f91\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4fdd\u5b58Turtle\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SCREENSHOT\u5de5\u5177\u4fdd\u5b58\u56fe\u5f62<\/p>\n<\/p>\n<p><p>Turtle\u56fe\u5f62\u662f\u663e\u793a\u5728\u4e00\u4e2a\u7a97\u53e3\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u622a\u56fe\u5de5\u5177\u6765\u4fdd\u5b58\u8fd9\u4e2a\u7a97\u53e3\u7684\u5185\u5bb9\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5b83\u4fdd\u5b58\u7684\u56fe\u50cf\u662f\u4f4d\u56fe\u683c\u5f0f\uff0c\u653e\u5927\u540e\u53ef\u80fd\u4f1a\u5931\u771f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u81ea\u5e26\u7684\u622a\u56fe\u5de5\u5177\uff1a\u5728\u7ed8\u5236\u5b8c\u6210\u540e\uff0c\u624b\u52a8\u4f7f\u7528Windows\u81ea\u5e26\u7684\u201c\u622a\u56fe\u5de5\u5177\u201d\u6216Mac\u7684\u201cCommand + Shift + 4\u201d\u8fdb\u884c\u622a\u56fe\uff0c\u7136\u540e\u4fdd\u5b58\u4e3a\u6240\u9700\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528Python\u5e93\u622a\u56fe\uff1a\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u8bf8\u5982Pillow\u6216pyautogui\u7b49\u5e93\u8fdb\u884c\u7a0b\u5e8f\u5316\u622a\u56fe\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from turtle import *<\/p>\n<p>from PIL import ImageGrab<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>turtle = Turtle()<\/p>\n<p>turtle.forward(100)<\/p>\n<p>turtle.left(90)<\/p>\n<p>turtle.forward(100)<\/p>\n<h2><strong>\u622a\u56fe<\/strong><\/h2>\n<p>screen = getscreen()<\/p>\n<p>canvas = screen.getcanvas()<\/p>\n<p>x0 = screen.window_width() \/\/ 2<\/p>\n<p>y0 = screen.window_height() \/\/ 2<\/p>\n<p>ImageGrab.grab(bbox=(0, 0, x0, y0)).save(&quot;turtle_image.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528CANVAS\u65b9\u6cd5\u4fdd\u5b58\u56fe\u5f62<\/p>\n<\/p>\n<p><p>Turtle\u6a21\u5757\u7684Screen\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e2agetcanvas\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u56fe\u5f62\u7684\u753b\u5e03\u5bf9\u8c61\u3002\u501f\u52a9\u8fd9\u4e2a\u753b\u5e03\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06Turtle\u56fe\u5f62\u4fdd\u5b58\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4fdd\u5b58\u4e3aPostScript\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528PostScript\u683c\u5f0f\u4fdd\u5b58\u662fTurtle\u56fe\u5f62\u4fdd\u5b58\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662f\u77e2\u91cf\u683c\u5f0f\uff0c\u80fd\u591f\u4fdd\u8bc1\u56fe\u5f62\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from turtle import Screen, Turtle<\/p>\n<h2><strong>\u521d\u59cb\u5316<\/strong><\/h2>\n<p>screen = Screen()<\/p>\n<p>turtle = Turtle()<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>turtle.forward(100)<\/p>\n<p>turtle.left(90)<\/p>\n<p>turtle.forward(100)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aPostScript\u6587\u4ef6<\/strong><\/h2>\n<p>canvas = screen.getcanvas()<\/p>\n<p>canvas.postscript(file=&#39;turtle_image.eps&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06PostScript\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff08\u5982PNG\u3001JPEG\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528ImageMagick\u6216Ghostscript\u5de5\u5177\u8fdb\u884c\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528ImageMagick\u8fdb\u884c\u8f6c\u6362\u7684\u793a\u4f8b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code>convert turtle_image.eps turtle_image.png<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3aPDF\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5c06Turtle\u56fe\u5f62\u76f4\u63a5\u4fdd\u5b58\u4e3aPDF\u6587\u4ef6\uff0c\u53ef\u4ee5\u5229\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93ReportLab\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5ReportLab\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u5b89\u88c5ReportLab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528ReportLab\u4fdd\u5b58\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from turtle import Turtle, Screen<\/p>\n<p>from reportlab.pdfgen import canvas<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>screen = Screen()<\/p>\n<p>turtle = Turtle()<\/p>\n<p>turtle.forward(100)<\/p>\n<p>turtle.left(90)<\/p>\n<p>turtle.forward(100)<\/p>\n<h2><strong>\u521b\u5efaPDF<\/strong><\/h2>\n<p>c = canvas.Canvas(&quot;turtle_image.pdf&quot;)<\/p>\n<p>c.drawImage(&quot;turtle_image.eps&quot;, 0, 0)<\/p>\n<p>c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4fdd\u5b58Turtle\u56fe\u5f62\u7684\u591a\u79cd\u9009\u62e9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u4e00\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u56fe\u5f62\u7684\u7528\u9014\u548c\u6240\u9700\u7684\u683c\u5f0f\u3002\u5728\u4fdd\u5b58\u56fe\u5f62\u65f6\uff0c\u8bf7\u6ce8\u610f\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u6839\u636e\u9700\u8981\u8c03\u6574\u56fe\u5f62\u7684\u5c3a\u5bf8\u548c\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528turtle\u6a21\u5757\u4fdd\u5b58\u7ed8\u56fe\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>turtle.getcanvas().postscript(file=&quot;filename.eps&quot;)<\/code>\u6765\u4fdd\u5b58\u5f53\u524d\u7ed8\u56fe\u4e3aEPS\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8c03\u7528\u6b64\u65b9\u6cd5\u4e4b\u524d\u5b8c\u6210\u6240\u6709\u7684\u7ed8\u56fe\u64cd\u4f5c\u3002\u4e4b\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u8f6c\u6362\u5de5\u5177\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u5982PNG\u6216JPEG\u3002<\/p>\n<p><strong>turtle\u7ed8\u56fe\u4fdd\u5b58\u7684\u6587\u4ef6\u683c\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>turtle\u6a21\u5757\u672c\u8eab\u4e3b\u8981\u652f\u6301\u4fdd\u5b58\u4e3aEPS\u683c\u5f0f\u3002\u5982\u679c\u9700\u8981\u5176\u4ed6\u683c\u5f0f\uff0c\u53ef\u4ee5\u5c06EPS\u6587\u4ef6\u8f6c\u6362\u4e3aPNG\u3001JPEG\u7b49\u683c\u5f0f\uff0c\u4f7f\u7528\u56fe\u50cf\u7f16\u8f91\u8f6f\u4ef6\u6216\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5982ImageMagick\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728turtle\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u6587\u672c\u5e76\u4fdd\u5b58\uff1f<\/strong><br \/>\u5728turtle\u7ed8\u56fe\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>turtle.write(&quot;\u6587\u672c\u5185\u5bb9&quot;)<\/code>\u65b9\u6cd5\u5728\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u3002\u5b8c\u6210\u7ed8\u56fe\u548c\u6587\u672c\u6dfb\u52a0\u540e\uff0c\u4f7f\u7528\u4fdd\u5b58\u65b9\u6cd5\u4fdd\u5b58\u56fe\u50cf\uff0c\u786e\u4fdd\u6587\u672c\u4f1a\u5305\u542b\u5728\u4fdd\u5b58\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5c06\u56fe\u5f62\u4fdd\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528screenshots [&hellip;]","protected":false},"author":3,"featured_media":985373,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985368"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=985368"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985368\/revisions"}],"predecessor-version":[{"id":985376,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985368\/revisions\/985376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}