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

2024-09-27 19:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我用PyAutoGui进行了一次成功的试用,编写了一些粗糙的代码,帮助我完成一些重复的、单调的上传,这些都是为了我妻子的生意而经常做的。尽管很丑,但效果很好。在

为了进一步完善它,我在包含if/then语句之后才开始接收错误消息。我添加了if/then语句,以便它跳过空目录。我想确定到底是什么问题。我在Windows10上使用Python3.4。在

这是密码(我用了时间。睡觉()命令,因为某些命令需要一段时间才能执行,尤其是浏览器中的命令):

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的定义:

^{pr2}$

以下是我在运行包含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.

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

我很抱歉这么久才进来。任何帮助都将不胜感激。在


Tags: thetoiniftimesleepwindowdirectory

热门问题