Pyautogui click() and moveTo() not working mac os bigsur - python

I've been trying to use the pyautogui library but when i try to use the move() the python launcher bounces on the dock then disappears and nothing happens, i don't even get any errors, I saw a post from a while back when catalina was around and the person who made the post ended up going back to an older OS X because they couldn't figure out a solution, i also found the problem on the github repo for pyautogui but the solution which was to add your terminal or python launcher or code editor to accessibility list in privacy didn't work for me. Below i'm providing my code, the github issue link and the other person's post, by the way i've done everything they said they've done. I'd really appreciate some help, thanks.
Is anyone else having PyAutoGUI issues after updating to Catalina?
pyautogui.click() almost seems to work. Clicks work in the window that
I tell it to, but it's as if immediately after a click, the window
focus returns to VS Code or Terminal. And then any
pyautogui.typewrite, keyup, keydown, any of those keystroke commands
are entered into VS Code or Terminal rather than the window I've had
it click on to focus. Doing a command + tab doesn't do the trick
either as those keys are entered into Terminal rather than acting as
hotkeys on OSX. I can't seem to keep the focus on the window I'm
trying to operate on. I've tried reverting from zsh back to bash. I've
tried adding Terminal, zsh, bash, and VS Code in the Accessibility
area of Security & Settings to give full keyboard and mouse control.
Apple does say that their depreciating Quartz (what pyautogui is built
off of to operate OSX) in 10.15 (realized this one too little, too
late). But that's supposed to mean it still works they just won't be
updating it, right? Anyone else experiencing this? I'm hoping for
someone smarter than me who can guide me to a fix. Thanks!
import os, sys, subprocess
import pyautogui
import time
def open_file(filename):
if sys.platform == "win32":
os.startfile(filename)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, filename])
def runZoom():
open_file("/Applications/zoom.us.app")
joinbtn=pyautogui.locateCenterOnScreen("Join_Meeting.png", confidence=.5)
pyautogui.moveTo(joinbtn)
pyautogui.click()
runZoom()
Github issue link:https://github.com/asweigart/pyautogui/issues/247

Try to use PyDirectInput module.
pip install pydirectinput

I was having trouble until I enabled access to accessibility features, thanks!

I also ran into this issue on my mac, you have to manually add Python Launcher to the list of apps that can control your computer.
System preferences --> Security & Privacy --> Accessibilty --> +Python Launcher
i also added terminal to the list.

Related

Python: detect keyboard before other processes

I wrote a Python script that performs simple actions based on what I press on my keyboard.
The script always works perfectly, except when Visual Studio Code is open and active.
It's like VSCode is catching the pressed keys BEFORE my python script.
If I close VSCode, or just minimize the window, and another window is active, my python script works again.
VSCode does not "steal" the keys only from python scripts, it steals them from other applications as well. For example, when VSCode window is active, I can not use my OBS shortcut for start recording.
I tried to lower VSCode priority and increase my python script priority, but it did not work.
Does anyone know how can I make my python script catch the pressed keys, before VSCode steals them?
EDIT:
Please find below a minimum reproducible example.
The following script prints an a when the a key is pressed on the keyboard. It works with any active window, except VSCode. In fact, when VSCode window is active it stops working.
Tests done in Windows 10.
from keyboard import is_pressed
from time import sleep
while True:
if is_pressed('a'):
print('a')
sleep(0.2)
I have found the solution!
I noticed that my VSCode was set to always run as administrator. I set this option months ago, and somehow persisted even after completely uninstalling and reinstalling VSCode.
I just disabled the option and now it works!
Thank you ColdFish and MingJie for all of your help.
Thanks for the detail Jeffrey. I could not reproduce the issue on a reasonably fresh install of VSCode, also on Windows 10.
I looked through the documentation in the readme of the keyboard package and found this in the "Known Limitations" section:
Other applications, such as some games, may register hooks that swallow all key events. In this case keyboard will be unable to report events.
I'm going to hazard a guess that one of your VSCode plugins is registering a hook to capture key events. I suspect that if you try it on a fresh VSCode installation (which will be similar to mine) it may work.
Alternatively, you can try using another similar implementation in pynput of the same functionality. That may work without any alterations to VSCode. A minimum example is here that will mirror the functionality of your minimum example:
from pynput.keyboard import Key, Listener, KeyCode
def on_press(key):
if key == KeyCode.from_char('a'):
print('{0} pressed'.format(key))
while True:
with Listener(on_press=on_press) as listener:
listener.join()
You can find further documentation on how to handle the keyboard with pynput here.

How can I make an auto clicker with pyautogui only when I activate it

I have been wanting to make this for a while and have finally gotten around to it. But could not get it to work. I need it to be able to work on macOS Linux and windows, I also need it to be able to be deactivated and activated with the press of a button.
This is what I have tried.
`
import pyautogui
import time
while True:
cookie = pyautogui.locate("cookie.png")
pyautogui.click("cookie.png")
`
First, you should check if "cookie.png" looks the same in macOS, Linux and Windows. If it does not then you may need two or three different .png
Second, the code is quite ok. If you want to stop the clicking then you can use the library keyboard (better than pyautogui for keyboard control). Then you can define the keyword to stop the clicking.

Stop program from closing in python

So I made a program that uses the pyautogui module to type "Hello" over and over again in a while loop and press enter. It's not a console output. It works perfectly fine when I launch it from the pycharm editor but when I open it from the folder or convert to an exe it just closes instantly. Why does the program close when I try opening it from the file explorer or after converting it to an exe?
I'm new to python, thanks for any help!
import pyautogui
import time
message = "Hello"
time.sleep(5)
while True:
pyautogui.typewrite(message)
pyautogui.press("enter")
time.sleep(37)
After pressing Run I have 5 seconds to switch to a notepad and it will start typing "Hello" and enter every 37 seconds.
For anyone having the same problem on a Mac, you need to go to your security preferences.
Settings > Security & Privacy > Privacy > Accessibility
and allow that specific application to access your computer by pressing the + icon. However, in the question, you stated that you are using .exe files, so I will assume you are on Windows. I don't use Windows, but for people reading this question in the future on Mac, I hope this helps.
I have finally solved the problem!!
I ran my python script via the windows command prompt. The error it showed me was an error with a module named pyautogui. The problem was that my module was installed with the wrong version. Using "pip3" instead of "pip" install pyautogui fixed the problem for me and now I can open and use my script directly from the file explorer.

No Windows popping up while using pygame

I have been using pycharm in my mac but while importing pygame, there is no window popping up. I have copied some codes from google about pygames but it is also not working. I think there might some settings problem in mac os because in my pc the code is working perfectly. Another information is that no error is showing, which means no problem with code or interpreter.
For me, and other people, Python 3.8 doesn't work with PyGame for some reason. Try using Python 3.7.
I am also using PyCharm...
I found, as a workaround, on Apple,
if your "Force Quit" the running Python in the Dock,
and then try to run it again you get:
Whichever one your choose, it then continues and displays the window.

TKinter (filedialog.askdirectory) freezing Spyder console

I noticed in other questions that there are (or were) several problems with TKinter in Spyder. I have been using it in IDLE for a while but I am moving to Spyder, and came upon some problems.
I am running Python 3.6.4 with Spyder 3.2.8 from Anaconda 1.8.4, on Windows 7 Enterprise.
When I try to use some TKinter functions (like filedialog.askdirectory) Spyder´s console freeze.
I´ve been reading different forums but still no one has the same problem or a solution to this problem.
Here is a simple code that would work in IDLE, but not in SPYDER:
import os
from tkinter import Tk, filedialog
Tk().withdraw()
print("Done WITHDRAW")
currentdir= os.getcwd()
print("Done GETCWD")
filename= filedialog.askdirectory(title="Select folder", initialdir=currentdir)
print("Done ASKDIRECTORY")
As a result, I get:
runfile('M:/Users/KPK2/.../hello.py', wdir='M:/Users/KPK2/...')
Done WITHDRAW
Done GETCWD
And the console keeps running, waiting for the ASKDIRECTORY to pop a new window to select a file. In IDLE it works just fine.
Does anyone know which could be the problem and some possible solution?
I read on other threads solutions like updating to Spyder 3.0 (I already have 3.2.8) or changing some "External Modules" for the "Console" in Preferences, but there is not such a tab on my Prefereneces window (don´t know how to do that otherwise).
Thank you.
try running this command %gui tk at the console before running your code.
The alternative is it go to Tools > Preferences > IPython Console > Graphics > Graphics backend and select tkinter there.
This worked for me :)
The answer was found here https://groups.google.com/forum/#!topic/spyderlib/rFJhJZgjZTE

Categories

Resources