Python: PyautoGui Mouse Click Not working neither locateScreen based on screenshot - python

Below code stops after printing hello and nothing happens.
Tried different option as pyautogui.leftClick(993, 578) but no luck.
In below code if I don't use subprocess but keep zoom GUI open then click is working fine.
import subprocess
import pyautogui
import time
import os
import pandas as pd
from datetime import datetime
print("hello")
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(993, 578, 0)
pyautogui.leftClick()
print(1)

The simple problem i see with your code is this line:
subprocess.call("C:\\Users\xyz\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
You've missed a \ before the xyz.
The following works perfectly for me on Python 3.6+:
import pyautogui
import time
import os
print("begun")
os.system(r"C:\Users\xyz\AppData\Roaming\Zoom\bin\Zoom.exe")
time.sleep(5)
pyautogui.moveTo(pyautogui.locateCenterOnScreen('my image'))
pyautogui.click()
print("dun")
If this doesn't work, try the pydirectinput module too, it's the updated version of pyautogui with much the same functions.

Related

play defined area without delaying loop

from pyautogui import *
from threading import Timer
import pyautogui
import pydirectinput
import time
import keyboard
import random
import win32api, win32con
start = time.time()
def b()
time.sleep(0.30)
pydirectinput.press('b')
print("i see it")
while True:
if pyautogui.locateOnScreen('b.png', region=(1717,1127,150,150), grayscale=True, confidence=0.8):
b()
else:
print("cant see anything")
I'm still quite new to programming and even newer to python but I tried to get the script to detect an image, once it detects the image to play def b() which would press the b key after a few seconds. The only problem is, I'm trying to get the code to still search for the image while the def b() is still playing and to play it again if it detects that image again during that short time frame.

Why does the way pynput.mouse.Controller is imported matter?

I am using pynput.mouse.Controller to listen for certain mouse actions and also using it to navigate to certain targets.
When I import Controller as follows: from pynput.mouse import Controller everything works fine and the programs runs smoothly.
However, when I do this instead import pynput.mouse.Controller I receive an error telling ModuleNotFoundError: No module named 'pynput.mouse.Controller'
Unless I am having a fundemental misunderstanding, these lines should function the same. Is there any reason why one produces an error but the other doesn't?
import imports modules or packages (directories with __init__.py), it cannot import objects from modules. This doesn't work:
import pynput.mouse.Controller
This work:
import pynput.mouse
Controller = pynput.mouse.Controller
This also work:
from pynput input mouse
Controller = mouse.Controller
And this:
from pynput.mouse import Controller

How to avoid lock screen under Win10 via Python code? [duplicate]

I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
x = 1
while x == 1:
pyautogui.moveRel(1)
pyautogui.moveRel(-1)
time.sleep (300)
Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:
import pyautogui
import time
while True:
pyautogui.press('volumedown')
time.sleep(1)
pyautogui.press('volumeup')
time.sleep(5)
You can use any other keys if you want.
import ctypes
# prevent
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
# set back to normal
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
Tested on python 3.9.1, win 10 64bit

How to write a string with win32api and win32con in python 3.4.2

Trying to create a program that acts like a real life user, so I am using win32api and win32con for mouse movements
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
However I also want it to be able to fill out strings, I have been trying to look online but no luck, so really in short. The question is, how can I use these modules to write a string to for example an online form?
Thanks in advance!
import pyautogui as g
g.click(x=100, y=150) # click text box
g.typewrite('Hello world!') #type something...
g.click(x=200,y=100) # click ok
maybe ... this uses the third party pyautogui library

python-wnck set focus to window

I'm trying to set focus to a window using python-wnck.
The only docs I could find related to this library are from https://developer.gnome.org/libwnck/stable/WnckWindow.html
Using some code I found on another question here at SO, I was able to search for windows using the window title, but I'm not sure how to get a window to focus. From the above docs I found the function:
wnck_window_activate(WnckWindow *window, guint32 timestamp);
So in python I tried using this function like "window.activate(0)", but this appears to fail, the icon on my taskbar flashed but it doesn't get focus.In the terminal I get the message:
(windowTest.py:17485): Wnck-WARNING: Received a timestamp of 0; window activation may not function properly
So I think I may actually need to put in a valid timestamp but not sure how to get this.
This is the code Im using sofar:
import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
titlePattern = re.compile('.*Geany.*')
windows = screen.get_windows()
for w in windows:
if titlePattern.match(w.get_name()):
print w.get_name()
w.activate(0)
The solution was actually pretty simpleI just needed to "import time" then pass "int(time.time())" into the activate function
Working code:
import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys
import time
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
titlePattern = re.compile('.*Geany.*')
windows = screen.get_windows()
for w in windows:
if titlePattern.match(w.get_name()):
print w.get_name()
w.activate(int(time.time()))
To get away from the Wnck-WARNING, you need to send a valid timestamp with the w.activate() function. The way that I found to do this is to use:
now = gtk.gdk.x11_get_server_time(gtk.gdk.get_default_root_window())
w.activate(now)
There really should be an easier way to do this, or wnck should allow a timestamp of 0 to mean now like most of the gtk libraries use.

Categories