{"id":1022,"date":"2023-06-07T17:54:14","date_gmt":"2023-06-07T09:54:14","guid":{"rendered":"http:\/\/xinyixx.com\/?p=1022"},"modified":"2023-06-07T18:00:16","modified_gmt":"2023-06-07T10:00:16","slug":"python9","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/06\/07\/python9\/","title":{"rendered":"python\u6559\u7a0b9\uff1a\u5199\u4e00\u4e2a\u81ea\u52a8\u5316\u9f20\u6807\uff0c\u952e\u76d8\u8c03\u7528\u7a0b\u5e8f"},"content":{"rendered":"\n<p>\u81ea\u52a8\u5316\u9f20\u6807\uff0c\u952e\u76d8\u8c03\u7528\u7a0b\u5e8f\u7528\u5230\u7684\u6a21\u5757\u662fpyautogui\u3002<\/p>\n\n\n\n<p>pyautogui\u662f\u4e00\u4e2aPython\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u81ea\u52a8\u5316GUI\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u6a21\u62df\u9f20\u6807\u548c\u952e\u76d8\u7684\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5c4f\u5e55\u622a\u56fe\u548c\u56fe\u50cf\u8bc6\u522b\u7b49\u529f\u80fd\u3002\u901a\u8fc7pyautogui\uff0c\u7528\u6237\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u6765\u81ea\u52a8\u5316\u4e00\u4e9b\u91cd\u590d\u6027\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u6570\u636e\u5f55\u5165\u3001\u56fe\u50cf\u5904\u7406\u7b49\u3002<\/p>\n\n\n\n<p>pyautogui\u652f\u6301\u8de8\u5e73\u53f0\uff0c\u53ef\u4ee5\u5728Windows\u3001macOS\u548cLinux\u7b49\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\u5b83\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\uff0c\u5305\u62ec\u9f20\u6807\u548c\u952e\u76d8\u64cd\u4f5c\u3001\u5c4f\u5e55\u622a\u56fe\u3001\u56fe\u50cf\u8bc6\u522b\u3001\u5b9a\u65f6\u5668\u7b49\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u90e8\u5206\u81ea\u52a8\u5316\u9700\u6c42\u3002<\/p>\n\n\n\n<p>\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u9f20\u6807\u5b9a\u4f4d\u5728\u5c4f\u5e55\u4e2d\u592e\u548c\u70b9\u51fb\u7684\u6548\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p>import pyautogui<\/p>\n\n\n\n<p>pyautogui.moveTo(pyautogui.size().width \/ 2, <\/p>\n\n\n\n<p>pyautogui.size().height \/ 2)<\/p>\n\n\n\n<p>pyautogui.click()<\/p>\n<\/div>\n\n\n\n<p>\u548c\u4ee5\u524d\u4e00\u6837\uff0c\u9700\u8981\u5728\u7ec8\u7aef\u91cc\u4e0b\u8f7d\u6a21\u5757\uff1a<code>pip install pyautogui<\/code><\/p>\n\n\n\n<p>\u7a0b\u5e8f\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import pyautogui\nimport time\n\n# \u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\nx, y = 150, 150\nwidth, height = 500, 500\n\n# \u8bbe\u7f6e\u7ed8\u5236\u7684\u56fe\u5f62\u7684\u989c\u8272\u548c\u7ebf\u6761\u5bbd\u5ea6\ncolor = 'blue'\nthickness = 5\n\n# \u83b7\u53d6\u5c4f\u5e55\u7684\u5927\u5c0f\nscreenWidth, screenHeight = pyautogui.size()\n\n# \u70b9\u51fb\u5c4f\u5e55\u4e0a\u7684\u6307\u5b9a\u4f4d\u7f6e\u6253\u5f00\u7ed8\u56fe\u8f6f\u4ef6\npyautogui.click(x, y)\n\n# \u7b49\u5f85\u7ed8\u56fe\u8f6f\u4ef6\u52a0\u8f7d\u5b8c\u6210\ntime.sleep(1)\n\n# \u5c06\u9f20\u6807\u79fb\u52a8\u5230\u7ed8\u56fe\u533a\u57df\u7684\u5de6\u4e0a\u89d2\npyautogui.moveTo(x, y)\n\n# \u6309\u4e0b\u9f20\u6807\u5de6\u952e\u5e76\u62d6\u52a8\u5230\u7ed8\u56fe\u533a\u57df\u7684\u53f3\u4e0a\u89d2\npyautogui.dragTo(x + width, y, duration=0.5, button='left')\n\n# \u62d6\u52a8\u5230\u7ed8\u56fe\u533a\u57df\u7684\u53f3\u4e0b\u89d2\npyautogui.dragTo(x + width, y + height, duration=0.5, button='left')\n\n# \u62d6\u52a8\u5230\u7ed8\u56fe\u533a\u57df\u7684\u5de6\u4e0b\u89d2\npyautogui.dragTo(x, y + height, duration=0.5, button='left')\n\n# \u62d6\u52a8\u56de\u7ed8\u56fe\u533a\u57df\u7684\u5de6\u4e0a\u89d2\uff0c\u5f62\u6210\u4e00\u4e2a\u95ed\u5408\u56fe\u5f62\npyautogui.dragTo(x, y, duration=0.5, button='left')<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u8fd0\u884c\u8bd5\u4e00\u4e0b\u6548\u679c\uff0c\u5b9e\u73b0\u9f20\u6807\u5b9a\u4f4d\u548c\u62d6\u62fd\u6548\u679c\uff0c\u5177\u6709\u6761\u4ef6\u7684\u540c\u5b66\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>\u518d\u5199\u4e00\u4e2a\u952e\u76d8\u8c03\u7528\u7684\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import pyautogui\nimport time\n\n# \u6253\u5f00\u8bb0\u4e8b\u672c\u5e94\u7528\u7a0b\u5e8f\npyautogui.hotkey('win', 'r')\npyautogui.typewrite('notepad')\npyautogui.press('enter')\n\n# \u7b49\u5f85\u8bb0\u4e8b\u672c\u5e94\u7528\u7a0b\u5e8f\u6253\u5f00\ntime.sleep(2)\n\n# <\/code>\u6253\u51fa\u82f1\u6587\u540d\u8a00\uff0c\u8bb0\u5f97\u5c06\u8f93\u5165\u6cd5\u5207\u6362\u4e3a\u82f1\u6587\u3002<code>\npyautogui.typewrite('<\/code>To be, or not to be: that is the question.<code>\\nAsk not what your country can do for you, \\nask what you can do for your country.\\nI am the master of my fate,\\n I am the captain of my soul.\\n')\n\n# \u4fdd\u5b58\u6587\u4ef6\npyautogui.hotkey('ctrl', 's')\ntime.sleep(2)\npyautogui.typewrite('test')\npyautogui.press('enter')\n\n# \u5173\u95ed\u8bb0\u4e8b\u672c\u5e94\u7528\u7a0b\u5e8f\npyautogui.hotkey('alt', 'f4')<\/code><\/pre>\n\n\n\n<p>\u5f55\u4e86\u4e2a\u5c4f\u770b\u4e00\u4e0b\u6548\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1920 \/ 1080;\" width=\"1920\" controls src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/06\/11.mp4\"><\/video><\/figure>\n\n\n\n<p>\u4e0d\u77e5\u9053\u4ec0\u4e48\u539f\u56e0\u4f1a\u6709\u5361\u987f\uff0c\u53ef\u80fd\u662f\u5f55\u5c4f\u8f6f\u4ef6\u7684\u539f\u56e0\uff0c\u52a8\u4e86\u4e0b\u9f20\u6807\u5c31\u7ee7\u7eed\u5199\u4e86\u3002\u6309\u7167\u4e4b\u524d\u5199\u7684\u6559\u7a0b\u7528pyinstaller\u6253\u5305\u6210\u4e00\u4e2aexe\u6587\u4ef6\u5c31\u53ef\u4ee5\u81ea\u52a8\u5316\u5b8c\u6210\u91cd\u590d\u6027\u64cd\u4f5c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u52a8\u5316\u9f20\u6807\uff0c\u952e\u76d8\u8c03\u7528\u7a0b\u5e8f\u7528\u5230\u7684\u6a21\u5757\u662fpyautogui\u3002 pyautogui\u662f\u4e00\u4e2aPython\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10],"tags":[71,69,77],"class_list":["post-1022","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","tag-python","tag-learning","tag-coding","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=1022"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}