0

我最近成功地试用了 PyAutoGui,我编写了一些粗略的代码来帮助我完成一些重复、单调的上传,这些上传是我必须定期为我妻子的业务做的。尽管它很丑,但它运作良好。

为了稍微改进它,我只有在包含 if/then 语句后才开始收到错误消息。我添加了 if/then 语句,以便它跳过空目录。我试图确定问题到底是什么。我在 Windows 10 上使用 Python 3.4。

这是代码(我使用了 time.sleep() 命令,因为有些命令需要一段时间才能执行,尤其是浏览器内部的命令):

import pyautogui
from tkinter import *
import time
import os
master = Tk()

def Upload_All_Amelias():
    Select_Chrome()
    time.sleep(1)
    if os.listdir('D:\\Inventory\\Amelia\\XXS') != []:  #Checks to see if the directory is empty
        XXS_Amelias()
    else:
        pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
    time.sleep(1)
    if os.listdir('D:\\Inventory\\Amelia\\XS') != []:  #Checks to see if the directory is empty
        XS_Amelias()
    else:
        pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab
    time.sleep(1)
    if os.listdir('D:\\Inventory\\Amelia\\S') != []:  #Checks to see if the directory is empty
        S_Amelias()

def Select_Chrome():
    pyautogui.hotkey('Alt', 'Tab'),  #Selects Chrome

def XXS_Amelias():
    time.sleep(0.2),
    pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
    time.sleep(0.2),                #ZZZ
    pyautogui.click(),              #Clicks "Add Photos"
    time.sleep(1.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
    time.sleep(0.2),
    pyautogui.typewrite('D:\\Inventory\\Amelia\\XXS'),         #Pastes in correct directory path
    time.sleep(0.2),
    pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
    pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
    pyautogui.click(),              #Clicks in the middle of the directory window to select the window
    pyautogui.hotkey('Ctrl', 'a')   #Selects all
    time.sleep(0.2),                #ZZZ
    pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
    time.sleep(0.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab

def XS_Amelias():
    time.sleep(0.2),
    pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
    time.sleep(0.2),                #ZZZ
    pyautogui.click(),              #Clicks "Add Photos"
    time.sleep(1.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
    time.sleep(0.2),
    pyautogui.typewrite('D:\\Inventory\\Amelia\\XS'),         #Pastes in correct directory path
    time.sleep(0.2),
    pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
    pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
    pyautogui.click(),              #Clicks in the middle of the directory window to select the window
    pyautogui.hotkey('Ctrl', 'a')   #Selects all
    time.sleep(0.2),                #ZZZ
    pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
    time.sleep(0.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab

def S_Amelias():
    time.sleep(0.2),
    pyautogui.moveTo(1074,160),     #Moves cursor to "Add Photos"
    time.sleep(0.2),                #ZZZ
    pyautogui.click(),              #Clicks "Add Photos"
    time.sleep(1.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'l'),  #Selects Address Bar
    time.sleep(0.2),
    pyautogui.typewrite('D:\\Inventory\\Amelia\\S'),         #Pastes in correct directory path
    time.sleep(0.2),
    pyautogui.hotkey('Enter'),      #Hits enter to open correct directory
    pyautogui.moveTo(1000,600),     #Positions cursor in the middle of the directory window
    pyautogui.click(),              #Clicks in the middle of the directory window to select the window
    pyautogui.hotkey('Ctrl', 'a')   #Selects all
    time.sleep(0.2),                #ZZZ
    pyautogui.hotkey('Enter')       #Hits enter to upload all selected files
    time.sleep(0.5),                #ZZZ
    pyautogui.hotkey('Ctrl', 'Tab'), #Selects the Next tab

button = Button(master, text = "Upload All Amelias", command=Upload_All_Amelias, height = 5, width = 20)

button.pack()

mainloop()

同样,当我在没有 if/then 语句的情况下调用所有这些时,一切似乎都运行良好。但是,由于某种原因,当我使用它们时,它会崩溃。

这是代码工作时 Upload_All_Amelias 的定义:

def Upload_All_Amelias():
    Select_Chrome()
    time.sleep(1)
    XXS_Amelias()
    time.sleep(1)
    XS_Amelias()
    time.sleep(1)
    S_Amelias()

这是我使用包含的 if/then 语句运行它时收到的错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "C:/Users/Sam/Python Projects/UPLOAD_ALL_AMELIAS_5a.py", line 16, in Upload_All_Amelias
    XS_Amelias()
  File "C:/Users/Sam/Python Projects/UPLOAD_ALL_AMELIAS_5a.py", line 51, in XS_Amelias
    pyautogui.click(),              #Clicks "Add Photos"
  File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\__init__.py", line 362, in click
    platformModule._click(x, y, 'left')
  File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\_pyautogui_win.py", line 437, in _click
    _sendMouseEvent(MOUSEEVENTF_LEFTCLICK, x, y)
  File "C:\Python34\lib\site-packages\pyautogui-0.9.33-py3.4.egg\pyautogui\_pyautogui_win.py", line 480, in _sendMouseEvent
    raise ctypes.WinError()
FileNotFoundError: [WinError 18] There are no more files.

还有一点信息,它可以很好地通过第一个语句,然后在第二个语句上崩溃。但是除了它调用的目录之外,这些部分应该是相同的。

我很抱歉这么长的条目。任何帮助,将不胜感激。

4

0 回答 0