6

I am seeing a conflict between pyautogui and tkinter. When I run a program without tkinter, pyautogui can control the keyboard & mouse as expected. However, if I first get input (e.g. a user-selected directory) through tkinter, I get this error:

File "C:\Users\Test_Lab\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\__init__.py", line 362, in click
platformModule._click(x, y, 'left')
File "C:\Users\Test_Lab\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\_pyautogui_win.py", line 437, in _click
_sendMouseEvent(MOUSEEVENTF_LEFTCLICK, x, y)
File "C:\Users\Test_Lab\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
raise ctypes.WinError()
FileNotFoundError: [WinError 2] The system cannot find the file specified.

I can verify that tkinter gets a valid directory by printing it out first. Versions: Python= 3.5.1, tkinter.TkVersion=8.6, pyautogui.__version__='0.9.33', running on Windows 7 Pro.

Here is a minimal example that reproduces my issue:

import pyautogui
import tkinter
root=tkinter.Tk()
dirname = tkinter.filedialog.askdirectory(parent=root, initialdir="C:\\",
                                  title='Please select a directory')
dname=dirname.replace('/',os.sep)
print(dname)
root.quit()
root.destroy()
pyautogui.moveTo(35, 5)
pyautogui.click(pause=.1)
user3450049
  • 825
  • 1
  • 10
  • 20
  • 1
    For those encountering the same issue, I was able to avoid this problem with an ugly workaround. Put the pyautogui calls in a try block with a catch: pass. I'd still love the real answer if anyone knows. – user3450049 May 20 '16 at 06:53
  • 1
    This particular bug no longer happens in PyAutoGUI, as the code that raises this exception has been removed. It was caused by checks to the win32 api GetLastError(), but the keybd_event and mouse_event api calls don't update "last error", meaning that this would end up raising exceptions. – Al Sweigart Jan 14 '20 at 08:41

0 Answers0