简体   繁体   English

如何使用 python 在 windows GUI 程序中单击按钮?

[英]How can I click buttons in a windows GUI program using python?

I am writing a python script that automates running a program and performing different tasks within the program.我正在编写一个 python 脚本,它可以自动运行程序并在程序中执行不同的任务。 My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.我的主要问题是弄清楚如何单击按钮并与要控制的程序的 GUI 进行交互。

I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items.我目前正在使用 pyautogui 库并使用 pyautogui.click(X,Y) 来推进提示并单击不同的菜单和菜单项。 The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering.这种方法的问题在于,我依靠单独的脚本通过告诉我 cursor 悬停位置的坐标来通知我环境中感兴趣的坐标。 This probably will not work on other machines and just seems like a one case solution.这可能不适用于其他机器,并且似乎只是一种解决方案。

My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?我的问题是如何使用 windows 中的程序(点击)实现自动化,而不必对我需要点击的项目的确切 position 进行硬编码?

For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?例如,如果我需要单击“确定”框接受某些设置,如何让 Windows 抓取程序 window,通读选项并单击我需要的内容,而无需事先了解对话框的 Z4757FE07FD492A8BE0EA6A760D 和 where 683D6 “确定”按钮在哪里?

Code:代码:

import pyautogui as gui

gui.click(x,y)

The way you can do this using pyautogui is with their locating methods.您可以使用 pyautogui 执行此操作的方法是使用它们的定位方法。 You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates.您将需要一张图片(例如 OK 框),然后您可以让 pyautogui 在屏幕上找到它并为您提供它的坐标。 Check out the official documentation on this .查看有关此的官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中使用tkinter使用GUI对计算器进行编程? - How can I program a calculator with a GUI using tkinter in Python? 如何在python(GUI)上使用2个按钮执行两项不同的操作? - How can i do two different things with 2 buttons on python(GUI)? 如何使用运行模块功能在Komodo IDE中像在IDLE(Python GUI)中一样运行python程序? - How can I run a python program in Komodo IDE like in IDLE(Python GUI) using Run Module feature? 如何将GUI python程序编译为.exe - How can I compile a GUI python program into .exe 如何将我的 python gui 程序存储为桌面应用程序? - how can i store my python gui program as desktop app? 如何在Windows上准确编程自动的“单击”? - How can I accurately program an automated “click” on Windows? Appjar:如何使用按钮更改窗口? - Appjar: How can I change windows using buttons? 如何使用 Python 在特定的 windows 发送点击输入,例如在 chrome 或游戏中? - How can i send click inputs at specific windows like inside chrome or games using Python? Python 使用 Tkinter 的 GUI 扫雷游戏 如何正确显示在按钮上? - Python Minesweeper game with GUI using Tkinter How to display on buttons correctly? 如何从Python启动GUI程序,但是当用户关闭窗口时进程结束? - How can I start a GUI program from Python, but have the process end when the user closes the window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM