pyautogui works well when using it to click on the buttons (of a software ) on the screen
but is there any way to detect the change in button state, because when the first click is completed and the required task is accomplished then the button disappears and a new "yes" button appears and if the task is not accomplished then the "NO" button appears , the problem is , both buttons appear at the same x and y coordinates i mean either yes or no appears at that place , is there any way to find out if yes appeared or no appeared? and then clicking if yes appeared ?
You can use image recognition technique in pyautogui package.
You can use locateOnScreen function of pyautogui. First you can screenshot and save yes and no button image. You crop the image tightly around the button.
And then save them into "yes.png", and "no.png" respectively.
And then,
btnYesButton = None
btnNoButton = None
while btnYesButton == None and btnNoButton == None:
btnYesButton = pyautogui.locateOnScreen("yes.png")
btnNoButton = pyautogui.locateOnScreen("no.png")
if btnYesButton:
tmpCenter = pyautogui.center(btnYesButton)
pyautogui.click(tmpCenter)
Related
I am using pynput based listener to detect and record mouse clicks using python.
def on_click(x, y, button, pressed):
if pressed:
on_release("clicked")
button = str(button).replace("Button.","")
inverted_comma = "'"
button = f"{inverted_comma}{button}{inverted_comma}"
mouse_values = [x, y, button]
macro_writer('click',mouse_values)
#image based click
time.sleep(1)
pyautogui.moveTo(1,1)
time.sleep(2)
x = x-50
y = y-50
im3 = pyautogui.screenshot(r"D:\Library\Project\Project\theautomater\src\macro\prett2.png",region=(x,y, 100 , 100))
I am able to record the coordinates of the mouse. Problem is I want to record the image/icon where the mouse clicks and as you can see the last line, I can do that but it happens AFTER the click.
This creates the problem that the icon is in "clicked" or "hover" state.
The solution I am thinking of implementing is pausing the click function taking screenshot then clicking.
For this, I need to figure out how to delay the mouse click using python. Can anyone suggest something?
The other question on SO, does not work as intended (Delay mouse click 0.5 second), please do not mark as duplicate.
In the following image, I want Python (using module Pyautogui) to click the "OK" button.
The problem is, I dont know when the "OK" button appears. So how do I make python to click the "OK" button only when it appears?
References:
If you wonder it looks like a mobile screen, I am using an android emulator.
PyAutoGUI has a function called locateOnScreen() which takes a screenshot of your current screen and then looks for the provided image you pass into the parameter of the function.
First, you need to have a picture of the "OK" button to tell PyAutoGUI what to look for, which you can do using pyautogui.screenshot('ok.png', region=(0, 0, 300, 400)) the region parameter specifies where to look and the size of the picture.
After that, you can run locateOnScreen('ok.png') and if the picture 'ok.png' is currently on your monitor it will return the center coordinates, which you can then pass to pyautogui.click()
import pyautogui
pyautogui.screenshot('ok.png', region=(0, 0, 300, 400))
Run that line of code and play around with the region parameter until you get a good looking 'ok.png' and make sure only the button is visible in the picture as PyAutoGUI looks for an exact match. Once you're done, replace the line with the following:
import pyautogui
location = pyautogui.locateOnScreen('ok.png')
pyautogui.click(location)
Now, whenever the "OK" button is on your screen, PyAutoGUI will click on it
I am trying to create a car configurator using tkinter as a gui in my free time.
I have managed to open a tkinter box with images that act as buttons.
What I want to do is for the user to click on a button. I want to check which button has been clicked (i.e if the family car button is clicked, how can I check that it has been clicked).
I have done my research on this website, and all of the solutions I have found have been in javascript or other languages.
Once the button has been clicked, I want a new window to be opened ONLY containing attributes for a family car i.e a family car can have a red exterior colour, but a sports car cannot have a red exterior colour at all.
Here is my code below:
from tkinter import *
import tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
familycar = PhotoImage(file = "VW family car.png")
familylabel = Button(root, image=familycar)
familybutton = Button(root, image=familycar, command=create_window)
familybutton.pack()
So how can I check that the family car button has been clicked?
Thanks
Use a Boolean flag.
Define isClicked as False near the beginning of your code, and then set isClicked as True in your create_window() function.
This way, other functions and variables in your code can see whether the button's been clicked (if isClicked).
Not sure what you asked, do you want to disable it or check its status in another routine ?
Or just to count the times it has been clicked,
In order to do that Simple solution would be to add a general variable that will be updated inside the create_window method (general because you want to allow access from other places).
First, you would need to initialize a function in which you want to execute on the button click.
example:
def button_clicked():
print('I got clicked')
Then, when you define the target button, you'll have to set the 'command' argument to the required function(the button_clicked function in this context).
Hi I need to do this because, I am making a matching / memmory game, and there has to be a button (Totally separated from the ones on the current game) that when I press it, it has to show the matching cards automatically without having to touch the buttons with the mouse.
Is there a "press" function or something like that for pressing the button?
Thanks! :)
As Joel Cornett suggests in a comment, it might make more sense to simply call the callback that you passed to the button. However, as described in the docs, the Button.invoke() method will have the same effect as pressing the button (and will return the result of the callback), with the slight advantage that it will have no effect if the button is currently disabled or has no callback.
If you also want visual feedback for the button you can do something like this:
from time import sleep
# somewhere the button is defined to do something when clicked
self.button_save = tk.Button(text="Save", command = self.doSomething)
# somewhere else
self.button_save.bind("<Return>", self.invoke_button)
def invoke_button(self, event):
event.widget.config(relief = "sunken")
self.root.update_idletasks()
event.widget.invoke()
sleep(0.1)
event.widget.config(relief = "raised")
In this example when the button has focus and Enter/Return is pressed on the keyboard, the button appears to be pressed, does the same thing as when clicked (mouse/touch) and then appears unpressed again.
So I have a Python controller which has two sensors hooked up to it, a Mouse Left Button sensor and a Mouse Over sensor, both have TRUE level triggering enabled with a frequency of 0. The Python controller is linked to a script which is shown below:
# Gather information.
scene = GameLogic.getCurrentScene();
camera = scene.active_camera;
controller = GameLogic.getCurrentController();
# Change to Earth camera.
clicked = controller.sensors['MouseClick'].positive;
if clicked:
hitObject = controller.sensors['MouseOver'].hitObject;
if hitObject is not None:
print(hitObject.name);
if(hitObject.name == 'Earth'):
scene.active_camera = 'Earth Camera';
else:
print('Nothing hit!');
Basically, it is supposed to check if the mouse's left button was clicked, and if so grab the hitObject of the mouse over sensor and print out the name of it, if the mouse over object is None, it prints out "Nothing hit!". However, in the game when I click on an object (such as the Earth, which is hard to miss clicking on) all it does is print out, "Nothing hit!".
Is there something wrong with the way I am using the sensors? Is my Python incorrect here? Why is controller.sensors['MouseOver'].hitObject always of type None?
the object, you want to click should not have the Physics Type 'No Collision'. I know that 'Static' works.
Good luck.