0

I'm trying to run one script from another script. I've read; What is the best way to call a script from another script? and I can't seem to get this to work.

My main script (Script A) does a lot of image processing and GUI interactions. However, randomly an error message or other window might appear interrupting the GUI interactions until the message or window is closed.

I've written a second script (Script B) that I want to run perpetually that closes these windows or error messages when discovered.

I'm trying to call Script B from Script A like this:

import close_windows

close_windows.closeWindows
print("Starting Close Windows....")

And Script B is:

import pyautogui as py

def closeWindows():

    image = r'C:\image.jpg'
    image2 = r'C:\image2.jpg'

    while True:
        foundimage = py.locateCenterOnScreen(image) 
        foundimage2 = py.locateCenterOnScreen(image2) 
    
        if foundimage or foundimage2 != None:
            py.click(1887, 65)

When I run script B independently it works, when I try running it via Script A with close_windows.closeWindows nothing happens.

I've also tried from close_windows import closeWindows and calling closeWindows but again, nothing happens.

mak47
  • 303
  • 2
  • 14
  • you should **call** the function `close_windows.closeWindows`; i.e. `close_windows.closeWindows()`. – hiro protagonist Mar 23 '22 at 09:47
  • `close_windows.closeWindows` should be `close_windows.closeWindows()` – Cow Mar 23 '22 at 09:48
  • Everything below close_windows.closeWindows turns grey when I add the (). Is there a way to fix this? – mak47 Mar 23 '22 at 09:50
  • Probably because the while loop in closeWindows() does not stop. – Raphael Mar 23 '22 at 09:51
  • That makes sense, I assume then this approach isn't going to work at all! – mak47 Mar 23 '22 at 09:52
  • 1
    @mak47 not unless you have some way to break the while loop or not use it at all. – Cow Mar 23 '22 at 09:53
  • As a side note - `if foundimage or foundimage2 != None:` clearly shows there is misunderstanding how to test multiple variables against single value. Nevertheless it will not work - [as mentioned in the docs](https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions) _As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise `ImageNotFoundException` instead of returning `None`._. Finally, the `"Starting Close Windows...."` will be printed on screen nly after the function has actually finished. – buran Mar 23 '22 at 09:53
  • While that might not be the correct way to use it, it definitely works – mak47 Mar 23 '22 at 09:54
  • Sorry, but I trust the docs, unless you use pre-0.9.41 version. What happens if neither of images is located? – buran Mar 23 '22 at 09:58
  • @mak47 What is the use of the while loop? Cant you just remove it and check what you need to check and then py.click if they are not None? – Cow Mar 23 '22 at 09:58
  • If none of the images are located nothing happens. I'm happy to share the full script if you like, I use this all over the place and have been using it for about two years without issue. @buran – mak47 Mar 23 '22 at 09:59
  • 1
    @user56700 the main script runs 24/7 and the errors appear randomly, I can put the check-in against every single GUI interaction I suppose but I thought it would more sense just to have a script running a also 24/7 to deal with any random windows that open. It's probably case for multiprocessing I guess – mak47 Mar 23 '22 at 10:00
  • @mak47 I see, then I would run it in a separate thread using multiprocessing. – Cow Mar 23 '22 at 10:03
  • I've been trying to avoid multiprocessing but it is probably time! Thank you – mak47 Mar 23 '22 at 10:40

0 Answers0