1

I have a program that is always open and every now and again I need to update some background files for it but then I need to hit a key in the program to refresh it. I'm looking for a way to automate that refresh once the updating script has finished.

What i need to do:

1 - go to the open program on my computer which is called Program

2 - press ctrl + shift + r to perform the refresh

This was a script that I have tried but no such luck from within Pycharm:

import pyautogui
from time import sleep
import subprocess

p = subprocess.Popen(r'C:\Users\xxx\Desktop\xxx\Program.exe')
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
 

p.kill()

This script keeps me in pycharm and sends me to a find and replace task. Any ideas? thanks!

3
  • 1
    This is independent of PyCharm - PyCharm is merely an IDE and whatever solution you get would be the same in any regular Python IDE or editor. Commented Dec 19, 2020 at 1:17
  • ok thanks - do you have any sugestions as to how i would solve this problem? Do i need to run it straight in a python console or soemthing like that Commented Dec 19, 2020 at 1:19
  • The problem is you'll need the window to get focus - I'm no expert on the library, but this post suggests that pyautogui can't help there, so you'll need to find another way to set focus to the spawned window. stackoverflow.com/questions/43785927/… Commented Dec 19, 2020 at 1:20

2 Answers 2

1

There is a simple bypass to this, put your program to sleep for a few seconds till you focus on the window manually.

Here, I have tried this on StachOverflow and it worked!

import pyautogui
from time import sleep

sleep(5)
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("shift")
pyautogui.keyUp("ctrl")

After running the code, I switched to the tab and waited. Soon the webpage refreshed successfully!

Note: This is just a small bypass. I recommend you to look out on how to get other window in focus as suggested by Grismar

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, I actually found the answere here stackoverflow.com/questions/2090464/python-window-activation
0

The answer that worked for me is here to put the window in focus:

Python Window Activation

and then run the following:

pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press("r")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.