{"id":962185,"date":"2024-12-27T04:05:55","date_gmt":"2024-12-26T20:05:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962185.html"},"modified":"2024-12-27T04:05:58","modified_gmt":"2024-12-26T20:05:58","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80turtle%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962185.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00turtle\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104215\/359d8fbb-2cdb-4f24-9105-c0e730128f8f.webp\" alt=\"python\u5982\u4f55\u6253\u5f00turtle\u5e93\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5f00turtle\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165turtle\u6a21\u5757\u6765\u5b9e\u73b0\u3001\u4f7f\u7528turtle.setup()\u51fd\u6570\u8fdb\u884c\u57fa\u672c\u7a97\u53e3\u8bbe\u7f6e\u3001\u8c03\u7528turtle.done()\u4ee5\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\u3002<\/strong> <code>turtle<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u6848\uff0c\u7279\u522b\u9002\u5408\u521d\u5b66\u8005\u7528\u6765\u5b66\u4e60\u7f16\u7a0b\u548c\u7406\u89e3\u56fe\u5f62\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u548c\u4f7f\u7528turtle\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165turtle\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528turtle\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u3002\u901a\u5e38\uff0cPython\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86turtle\u5e93\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165turtle\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u884c\u4ee3\u7801\uff0cturtle\u5e93\u5c06\u88ab\u5bfc\u5165\u5230\u4f60\u7684Python\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5e93\u4e2d\u63d0\u4f9b\u7684\u5404\u79cd\u529f\u80fd\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165turtle\u5e93\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u7ed8\u56fe\u7a97\u53e3\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u8bbe\u7f6e\u3002<code>turtle.setup()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">turtle.setup(width=600, height=400, startx=100, starty=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>setup()<\/code>\u51fd\u6570\u5c06\u7a97\u53e3\u7684\u5bbd\u5ea6\u8bbe\u7f6e\u4e3a600\u50cf\u7d20\uff0c\u9ad8\u5ea6\u8bbe\u7f6e\u4e3a400\u50cf\u7d20\uff0c\u7a97\u53e3\u5de6\u4e0a\u89d2\u7684\u521d\u59cb\u4f4d\u7f6e\u4e3a\u5c4f\u5e55\u5750\u6807(100,100)\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u57fa\u672c\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165turtle\u5e93\u5e76\u8bbe\u7f6e\u7a97\u53e3\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u7ed8\u5236\u57fa\u672c\u56fe\u5f62\u4e86\u3002turtle\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\u6765\u7ed8\u5236\u76f4\u7ebf\u3001\u66f2\u7ebf\u548c\u5176\u4ed6\u5f62\u72b6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u65b9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">turtle.forward(100)  # \u5411\u524d\u79fb\u52a8100\u50cf\u7d20<\/p>\n<p>turtle.left(90)      # \u5de6\u8f6c90\u5ea6<\/p>\n<p>turtle.forward(100)  # \u5411\u524d\u79fb\u52a8100\u50cf\u7d20<\/p>\n<p>turtle.left(90)      # \u5de6\u8f6c90\u5ea6<\/p>\n<p>turtle.forward(100)  # \u5411\u524d\u79fb\u52a8100\u50cf\u7d20<\/p>\n<p>turtle.left(90)      # \u5de6\u8f6c90\u5ea6<\/p>\n<p>turtle.forward(100)  # \u5411\u524d\u79fb\u52a8100\u50cf\u7d20<\/p>\n<p>turtle.left(90)      # \u5de6\u8f6c90\u5ea6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u6301\u7a97\u53e3\u6253\u5f00<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u5b8c\u6240\u6709\u7ed8\u56fe\u64cd\u4f5c\u540e\uff0c\u901a\u5e38\u9700\u8981\u8c03\u7528<code>turtle.done()<\/code>\u6765\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\uff0c\u4ee5\u4fbf\u80fd\u591f\u67e5\u770b\u7ed8\u5236\u7684\u7ed3\u679c\u3002\u5982\u679c\u4e0d\u8c03\u7528<code>turtle.done()<\/code>\uff0c\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u5728\u7ed8\u56fe\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u7a97\u53e3\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528turtle\u7ed8\u5236\u590d\u6742\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u76f4\u7ebf\u548c\u7b80\u5355\u5f62\u72b6\u4e4b\u5916\uff0cturtle\u5e93\u8fd8\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u66f4\u590d\u6742\u7684\u56fe\u5f62\u548c\u56fe\u6848\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u548c\u51fd\u6570\u7ec4\u5408\u6765\u7ed8\u5236\u590d\u6742\u7684\u51e0\u4f55\u56fe\u6848\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2afor\u5faa\u73af\u6765\u7ed8\u5236\u4e00\u4e2a\u591a\u8fb9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sides = 6  # \u591a\u8fb9\u5f62\u7684\u8fb9\u6570<\/p>\n<p>length = 100  # \u6bcf\u6761\u8fb9\u7684\u957f\u5ea6<\/p>\n<p>for _ in range(sides):<\/p>\n<p>    turtle.forward(length)<\/p>\n<p>    turtle.left(360 \/ sides)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u5b9a\u4e49turtle\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>turtle\u5e93\u8fd8\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49\u753b\u7b14\u7684\u989c\u8272\u3001\u7c97\u7ec6\u4ee5\u53ca\u753b\u5e03\u7684\u80cc\u666f\u8272\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>turtle.pensize(width)<\/code>\uff1a\u8bbe\u7f6e\u753b\u7b14\u5bbd\u5ea6\u3002<\/li>\n<li><code>turtle.pencolor(color)<\/code>\uff1a\u8bbe\u7f6e\u753b\u7b14\u989c\u8272\u3002<\/li>\n<li><code>turtle.bgcolor(color)<\/code>\uff1a\u8bbe\u7f6e\u753b\u5e03\u80cc\u666f\u989c\u8272\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u8bbe\u7f6e\u753b\u7b14\u989c\u8272\u4e3a\u84dd\u8272\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u9ec4\u8272\uff0c\u5e76\u7ed8\u5236\u4e00\u6761\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">turtle.pensize(3)<\/p>\n<p>turtle.pencolor(&quot;blue&quot;)<\/p>\n<p>turtle.bgcolor(&quot;yellow&quot;)<\/p>\n<p>turtle.forward(150)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528turtle\u5e93\uff0cPython\u7a0b\u5e8f\u5458\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u6848\u3002\u8be5\u5e93\u7279\u522b\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7f16\u7a0b\u57fa\u7840\u548c\u56fe\u5f62\u7f16\u7a0b\u3002\u901a\u8fc7\u5bfc\u5165\u5e93\u3001\u8bbe\u7f6e\u7a97\u53e3\u3001\u7ed8\u5236\u56fe\u5f62\u5e76\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\uff0c\u7528\u6237\u53ef\u4ee5\u5728Python\u4e2d\u521b\u5efa\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u5404\u79cd\u56fe\u5f62\u3002turtle\u5e93\u7684\u6613\u7528\u6027\u548c\u7075\u6d3b\u6027\uff0c\u4f7f\u5176\u6210\u4e3a\u5b66\u4e60\u7f16\u7a0b\u548c\u56fe\u5f62\u8bbe\u8ba1\u7684\u7406\u60f3\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5turtle\u5e93\uff1f<\/strong><br \/>turtle\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u5728\u5b89\u88c5Python\u65f6\u4f1a\u81ea\u52a8\u5305\u542b\u3002\u5982\u679c\u4f60\u7684Python\u73af\u5883\u4e2d\u6ca1\u6709turtle\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u5b89\u88c5Python\u6765\u89e3\u51b3\u3002\u786e\u4fdd\u9009\u62e9\u5305\u542bturtle\u5e93\u7684\u5b8c\u6574\u5b89\u88c5\u9009\u9879\u3002\u6b64\u5916\uff0cturtle\u5e93\u53ef\u4ee5\u5728Python\u7684IDLE\u73af\u5883\u4e2d\u76f4\u63a5\u4f7f\u7528\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<p><strong>turtle\u5e93\u7684\u4e3b\u8981\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>turtle\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u7ed8\u56fe\u548c\u56fe\u5f62\u7f16\u7a0b\u7684\u6a21\u5757\uff0c\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7f16\u7a0b\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7b80\u5355\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u7ebf\u6761\u3001\u5f62\u72b6\u3001\u56fe\u6848\u7b49\u3002\u7528\u6237\u53ef\u4ee5\u63a7\u5236\u201c\u6d77\u9f9f\u201d\u5728\u5c4f\u5e55\u4e0a\u79fb\u52a8\uff0c\u6539\u53d8\u989c\u8272\u3001\u586b\u5145\u548c\u7b14\u7684\u7c97\u7ec6\uff0c\u4ee5\u521b\u5efa\u4e30\u5bcc\u7684\u56fe\u5f62\u4f5c\u54c1\uff0c\u975e\u5e38\u9002\u5408\u6559\u5b66\u548c\u8da3\u5473\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\uff1f<\/strong><br \/>\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u56fe\u5f62\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u5e93\uff0c\u63a5\u7740\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6d77\u9f9f\u5bf9\u8c61\u3002\u901a\u8fc7\u8c03\u7528\u5e93\u4e2d\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63a7\u5236\u6d77\u9f9f\u7684\u79fb\u52a8\u548c\u7ed8\u5236\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>turtle.forward(100)<\/code>\u53ef\u4ee5\u8ba9\u6d77\u9f9f\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d\uff0c<code>turtle.right(90)<\/code>\u5219\u53ef\u4ee5\u8ba9\u6d77\u9f9f\u53f3\u8f6c90\u5ea6\u3002\u5229\u7528\u8fd9\u4e9b\u57fa\u672c\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u7ec4\u5408\u51fa\u590d\u6742\u7684\u56fe\u5f62\u548c\u56fe\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5f00turtle\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165turtle\u6a21\u5757\u6765\u5b9e\u73b0\u3001\u4f7f\u7528turtle.setup()\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":962192,"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\/962185"}],"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=962185"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962185\/revisions"}],"predecessor-version":[{"id":962193,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962185\/revisions\/962193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962192"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}