pyautogui
是一个流行的 Python 库,用于自动化 GUI 交互
以下是在多个显示器上使用 pyautogui
的一些方法和示例:
import pyautogui
screen_sizes = pyautogui.size()
print(screen_sizes)
这将返回一个元组,包含所有显示器的尺寸(宽度和高度)。
import pyautogui
# 获取第一个显示器的尺寸
screen_width, screen_height = pyautogui.size()
# 计算第二个显示器的坐标(假设它位于第一个显示器的右侧)
second_screen_x = screen_width
second_screen_y = 0
# 移动鼠标到第二个显示器的中心
pyautogui.moveTo(second_screen_x + screen_width / 2, second_screen_y + screen_height / 2)
import pyautogui
screen_sizes = pyautogui.size()
total_width = sum([size[0] for size in screen_sizes])
total_height = max([size[1] for size in screen_sizes])
for screen_width, screen_height in screen_sizes:
# 计算当前显示器的中心坐标
x = sum([size[0] for size in screen_sizes[:screen_sizes.index((screen_width, screen_height))]) + screen_width / 2
y = max([size[1] for size in screen_sizes[:screen_sizes.index((screen_width, screen_height))]) + screen_height / 2
# 在当前显示器的中心执行操作
pyautogui.moveTo(x, y)
pyautogui.click()
这个示例将遍历所有显示器,并在每个显示器的中心点击鼠标。
注意:在多显示器设置中,确保 pyautogui
可以正确检测到所有显示器。有时,这需要在操作系统的显示设置中启用“扩展这些显示器”选项。此外,pyautogui
的行为可能因操作系统和硬件的不同而有所差异。
领取专属 10元无门槛券
手把手带您无忧上云