当包含if / then语句时,PyAutoGui崩溃

时间:2017-01-20 15:08:39

标签: python tkinter pyautogui

我最近与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.

再多一点信息,它通过第一个语句很好,然后在第二个崩溃。但这些部分应该是相同的,除了它正在调用的目录。

我很抱歉这么长时间的进入。任何帮助将不胜感激。

0 个答案:

没有答案