[파이썬] pyautogui 다중 모니터에서 작동

image

When working with multiple monitors, it can be a challenge to automate tasks or perform actions on a specific monitor using the pyautogui library in Python. In this blog post, we will explore how to handle multiple monitors and ensure that our automation scripts work seamlessly across all screens.

Detecting Multiple Monitors

The first step is to detect the number of connected monitors in the system. Pyautogui provides a method called pyautogui.screeninfo.get_monitors() that returns information about each monitor connected to the system. Here’s an example code snippet:

import pyautogui

monitors = pyautogui.screeninfo.get_monitors()
print(f"Number of monitors: {len(monitors)}")

This code will print the number of monitors detected in the system. You can use this information to determine how many screens you need to work with in your automation script.

Selecting a Specific Monitor

Once we know the number of monitors, we can select a specific monitor to perform actions on. The monitors are indexed starting from 0, where 0 represents the primary monitor. To select a specific monitor, we can use the pyautogui.moveTo() and pyautogui.click() functions.

Here’s an example code snippet that moves the mouse cursor to the center of the second monitor and clicks:

import pyautogui

monitors = pyautogui.screeninfo.get_monitors()
if len(monitors) > 1:
    monitor = monitors[1]  # select the second monitor
    x = monitor.left + monitor.width / 2
    y = monitor.top + monitor.height / 2
    pyautogui.moveTo(x, y)  # move mouse to the center of the second monitor
    pyautogui.click()  # perform a click on the second monitor

In this example, we retrieve the information of the second monitor (index 1) and calculate the coordinates of the center of the monitor. We then use pyautogui.moveTo() to move the mouse cursor to the calculated coordinates and pyautogui.click() to simulate a click on the selected monitor.

Screenshotting a Specific Monitor

Another common task is capturing screenshots of a specific monitor. By using the pyautogui.screenshot() function along with the monitor’s dimensions, we can capture screenshots of individual monitors.

import pyautogui

monitors = pyautogui.screeninfo.get_monitors()
if len(monitors) > 1:
    monitor = monitors[1]  # select the second monitor
    screenshot = pyautogui.screenshot(region=(monitor.left, monitor.top, monitor.width, monitor.height))
    screenshot.save("monitor_2_screenshot.png")  # save the screenshot

In this example, we capture a screenshot of the second monitor by specifying the region as the coordinates and dimensions of the monitor. The resulting screenshot is then saved as “monitor_2_screenshot.png”.

Conclusion

Working with multiple monitors in pyautogui is essential when automating tasks or performing actions on specific screens. In this blog post, we explored how to detect multiple monitors, select a specific monitor, and capture screenshots. By leveraging these techniques, you can create powerful automation scripts that work seamlessly across multiple monitors.

I hope you found this blog post helpful. Happy coding with pyautogui and multiple monitors!