<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wepo Programming on Medium]]></title>
        <description><![CDATA[Stories by Wepo Programming on Medium]]></description>
        <link>https://medium.com/@wepobid?source=rss-c4e27f99e6a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QklqqOMS-45gGrLBRxWCqw.png</url>
            <title>Stories by Wepo Programming on Medium</title>
            <link>https://medium.com/@wepobid?source=rss-c4e27f99e6a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 03:05:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wepobid/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PyAutoGUI：使用 Python 实现自动化]]></title>
            <link>https://medium.com/@wepobid/pyautogui-%E4%BD%BF%E7%94%A8-python-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E5%8C%96-2297af478b27?source=rss-c4e27f99e6a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2297af478b27</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Wepo Programming]]></dc:creator>
            <pubDate>Tue, 13 Feb 2024 10:44:02 GMT</pubDate>
            <atom:updated>2024-02-13T10:44:02.663Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pv_KEAqGQIps1uIrJu8J1g.jpeg" /></figure><p>PyAutoGUI 是一个 Python 库，允许您通过控制鼠标和键盘来自动化任务。这样，您可以创建机器人、宏等……</p><p><strong>让我向您介绍这个库</strong>。</p><p>开始使用 PyAutoGUI 首先，您需要安装 PyAutoGUI。可以通过 pip 轻松完成：</p><pre>pip install pyautogui</pre><p>一旦安装完成，您可以使用以下方法将 PyAutoGUI 导入到您的 Python 脚本中：</p><pre>import pyautogui</pre><p><strong>控制鼠标</strong></p><p>当涉及到控制 GUI 时，您主要做的事情是移动鼠标。使用 PyAutoGUI，您可以通过以下代码实现：</p><pre>pyautogui.moveTo(x, y)</pre><p>除了移动光标外，PyAutoGUI 还允许您模拟鼠标按钮点击。click() 函数使您能够在当前光标位置执行主按钮（左键）点击。要执行次按钮（右键）点击，您可以使用 rightClick() 函数。这些功能可以与 moveTo() 结合使用，在屏幕上的特定坐标处点击。例如：</p><pre>import pyautogui<br><br>pyautogui.moveTo(500, 500)<br>pyautogui.click()</pre><p>如果鼠标移动应该花费一些时间，您可以指定其持续时间（以秒为单位）：</p><pre>pyautogui.moveTo(200, 300, duration=1)</pre><p>PyAutoGUI 还提供了执行其他类型点击的功能，如双击、中键点击和拖拽。doubleClick() 函数允许您在当前光标位置模拟双击，而 middleClick() 函数执行中键点击。要拖动鼠标，您可以使用 dragTo() 函数，指定从当前位置拖动后的目标坐标。</p><p>除了鼠标移动和点击，PyAutoGUI 还提供了鼠标相关自动化的其他功能。您可以使用 position() 函数检索当前鼠标位置，它返回一个包含 x 和 y 坐标的元组。scroll() 函数允许您通过指定垂直和水平滚动的单位数来模拟鼠标滚动。</p><p>如果您想要根据鼠标当前位置执行操作，而无需首先获取当前位置，您也可以使用 moveRel 和 dragRel。</p><pre>    mouse_position = pyautogui.position()<br>    print(mouse_position)<br>    <br>    pyautogui.dragTo(mouse_position[0] + 100, mouse_position[1], duration=1)<br>    pyautogui.moveRel(0, 100, duration=1)<br>    pyautogui.dragRel(100, 0, duration=1)</pre><h3>控制键盘</h3><p>您可以使用键盘进行的主要操作是输入文本。PyAutoGUI 允许您使用 typewrite() 函数模拟输入：</p><pre>pyautogui.typewrite(&quot;Hello&quot;)</pre><p>如果您想在每次按键之间引入延迟，可以向 typewrite() 函数传递一个可选的 interval 参数。interval 指定在输入每个字符后等待的秒数。</p><pre>pyautogui.typewrite(&quot;Hello&quot;, interval=0.1)</pre><p>除了输入文本，PyAutoGUI 允许您使用 press() 和 release() 函数分别模拟按下和释放特定键。这些功能接受键名或键码作为输入。例如，要模拟按下 Enter 键，您可以使用以下代码：</p><pre>pyautogui.press(&quot;enter&quot;)</pre><p>并释放它</p><pre>pyautogui.release(&quot;enter&quot;)</pre><p>PyAutoGUI 还提供了方便的 keyDown() 和 keyUp() 函数，分别用于按住键不放和释放键。这些功能在模拟键盘快捷键时非常有用。</p><pre>pyautogui.keyDown(&quot;ctrl&quot;)<br>pyautogui.press(&quot;s&quot;)<br>pyautogui.keyUp(&quot;ctrl&quot;)<br><br># you just saved a document!</pre><h3>您刚刚保存了一个文档！</h3><h3>显示消息</h3><p>如果您想在自动化过程中提供信息或反馈，可以使用一些 PyAutoGUI 函数。</p><p>要使用 PyAutoGUI 显示消息，您可以使用 alert() 函数。此功能创建一个带有消息和 OK 按钮的弹出对话框，供用户确认。</p><pre>pyautogui.alert(&quot;Hello world!&quot;)</pre><p>alert() 函数会暂停程序执行，直到用户在对话框中点击 OK 按钮。</p><p>除了 alert() 函数，PyAutoGUI 还提供了 confirm() 函数。此功能显示一个带有消息和两个按钮：OK 和 Cancel 的对话框。用户可以选择任一选项，函数返回一个布尔值，指示用户的选择。</p><pre>result = pyautogui.confirm(&quot;Do you want to proceed?&quot;)<br><br>if result:<br>    print(&quot;Proceeding with the task...&quot;)<br>else:<br>    print(&quot;Task aborted.&quot;)</pre><p>最后，您可以使用 prompt() 让用户输入字符串：</p><pre>beverage = pyautogui.prompt(&quot;Tea or coffee?&quot;)</pre><h3>处理图像 如果您的自动化需求稍微复杂一些，您肯定需要识别屏幕上的图像或拍摄屏幕截图。PyAutoGUI 让您可以做到这一点。</h3><p>首先，您有 locateOnScreen() 函数。此函数接受图像文件路径作为输入，并搜索屏幕上该图像的存在。如果找到图像，该函数返回图像首次出现的坐标。</p><pre>image_location = pyautogui.locateOnScreen(&quot;image.png&quot;)</pre><p>如果您想定位多个相同的图像，您只需使用 locateAllOnScreen 替代。这个函数将返回一个生成器，为屏幕上找到的图像的所有位置。</p><p>或者，您可以使用 locateCenterOnScreen 来获取图像中心所在屏幕位置的 x 和 y 坐标。</p><pre>pyautogui.locateCenterOnScreen(&#39;image.png&#39;)</pre><p>如果没有找到图像，这些函数将返回 None。</p><p>最后，您可以使用 screenshot() 来拍摄屏幕截图。它返回一个 Pillow/PIL 图像对象。如果您为此函数提供一个路径，它将保存截图：</p><pre>pyautogui.screenshot(&quot;src/images/screenshot.png&quot;)</pre><p>示例：Cookie Clicker 自动化 这个库的一个完美用例是 cookie-clicker 游戏。您需要做的唯一操作是点击和购买升级。让我们将其翻译成 PyAutoGUI：</p><pre>def cookie_clicker_automation():<br>    cookie_location = pyautogui.locateCenterOnScreen(&#39;cookie.png&#39;)<br>    <br>    for i in range(10):<br>        pyautogui.moveTo(cookie_location)<br>    <br>        for j in range(100):<br>            pyautogui.click()<br>            time.sleep(0.05)<br>    <br>        upgrade_location = pyautogui.locateCenterOnScreen(&#39;upgrade.png&#39;)<br>        pyautogui.moveTo(upgrade_location)<br>        pyautogui.click()</pre><p><strong>最后说明 当谈到自动化时</strong>，有许多可能的解决方案。使用 Python 的优势是您可以访问整个库生态系统来构建一些真正复杂的东西，因此我只能推荐 PyAutoGUI！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2297af478b27" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>