pyautogui locateonscreen not finding pop up - python-3.x

Im trying to automate a program, we use all the time:
import pyautogui, time
import PIL
input("Press Enter to start")
print("Starting program in 5 seconds")
time.sleep(5)
#open edgewise
pyautogui.hotkey("win")
pyautogui.typewrite("edgewise")
pyautogui.hotkey("Enter")
time.sleep(20)
print("Checking for error message")
a = pyautogui.locateOnScreen("ok.png")
while True:
if a == None:
print("Not Found")
break
else:
print("Found")
new0 = a[0]
new0 = new0 + 10
new1 = a[1]
new1 = new1 + 10
pyautogui.moveTo(new0,new1,duration=1)
pyautogui.click()
break
input("Finished")
So the idea of the program is that it opens the software, which during the startup it flashes up an error message (sometimes) which requires you to click ok.
Now, this program works if the program (edgewise) has already started and the error message is on-screen before I start the python script. I have even given it a sleep timer to make sure the program has opened correctly and is displaying the message in time
Any ideas

It seems that PYAutoGUI locateonscreen function only works when the program is already started, I tried to build my script to do everything including starting the application, but this causes issues.
Hope this helps others

Related

loop to check for issues

i have this small program to assign issues to me, it works great on pycharm but when i compile it to exe with pyintaller it closes after the first run, ignoring the time.sleep and running again creating a loop to check for issues every 5 seconds.
why is that? how can i fix it?
import time
import winsound
from jira import JIRA
global lst_ignore
issue_var = ""
lst_ignore = []
def jira_login():
global user,token
user = 'user#cloud.com'
token = 'kj432hj43214YMzCyMLOe7682'
try:
options = {'server': 'https://cloud.atlassian.net'}
global jira
jira = JIRA(options=options, basic_auth=(user, token))
except Exception as e:
jira = ""
if '401' in str(e):
print("Login to JIRA failed. Check your username and password",e)
return jira
def check_issue():
jira_login()
size = 100000
start = 0 * 100000
search_query = 'status not in (Done, Closed,Canceled) and assignee is EMPTY and project = "Success" and reporter not in (57f778:f48131cb-b67d-43c7-b30d-2b58d98bd077)'
issues = jira.search_issues(search_query, start, size)
for issue in issues:
if issue not in lst_ignore:
winsound.Beep(2000, 1000)
issue.update(assignee={'accountId': '60f1d072a0de61930ad83770'})
print(issue, " : assinged to you!")
lst_ignore.append(issue)
def check():
check_issue()
time.sleep(5)
check()
if __name__ == '__main__':
check()
NEW ANSWER:___________________________________________________
(New info obtained from comment) If it says you don't have the module when running from the console, but it does from Pycharm, you probably have 2 different Interpreters. There are two steps you must take.
Using the terminal run: pip install jira
If it says it is already installed, uninstall using pip, and then reinstall.
Good Luck!
OLD ANSWER:____________________________________________________
It is hard to say. One thing I can tell you is that a recursive function is almost never a good idea.
So instead of:
def check():
check_issue()
time.sleep(5)
check()
if __name__ == '__main__':
check()
Try the following aproach:
RUNNING = True
def main():
while RUNNING:
check_issue()
time.sleep(5)
if __name__ == '__main__':
main()
After that you can implement that if some condition is met, it wil change the RUNNING constant to exit the program. Or, if it is a console-run program, then just use a keyboard-interrupt

Python code works first time, then it does not show any output, and after that it works again. Interactive Brokers API

I want to learn to use the API from Interactive Brokers. I have an account with them, I open the TWS platform and then I execute a code copied from their Youtube channel. It works fine the first time, but after that if I execute again I see this in the console:
"Process finished with exit code 0"
And nothing else.
Sometimes after one minute it works again, no idea what the hell is going on.
-I tried using "terminate" in the console and running again but sometimes it works and sometimes it doesn't.
-I was receiving some messages from Windows Defender but I updated it and that should not be the origin of the problem. EDIT (I get the same error from windows defender now, I am going to update again)
-I tried closing Pycharm and starting it again and it works the first time only, the second is empty, but if I wait a few minutes it works again, but not always. I know there are some limitations like a max number of requests per second, but in this particular case I am just asking for contract details so I guess that is not the issue.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
class TestApp (EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print ("Error: ", reqId, " ", errorCode, " ", errorString)
def contractDetails(self, reqId, contractDetails):
print ("contract details", reqId, " ", contractDetails)
def main ():
app = TestApp ()
app.connect("127.0.0.1", 7497, 0)
contract = Contract()
contract.symbol = "EUR"
contract.secType = "CASH"
contract.exchange = "IDEALPRO"
contract.currency = "USD"
app.reqContractDetails(1, contract)
app.run()
app.disconnect()
if __name__ == "__main__":
main()
EDIT:
Let me add that this code always works fine, just checks the connection:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
app.run()
EDIT 2:
This is really frustrating, now nothing works, the console output is nothing, it is empty. I still get the windows defender message even after excluding the folders.
EDIT 3:
I reinstalled Pycharm 2019 and now I have 2020 and I get the output. Part of the problem of my code is the lack of a call to the function "disconnect()" to stop the run loop. Be careful with that folks. Brian's posts were very useful.

Python program stops after for-loop with input inside it is finished

Im writing the code in text editor and execute it by double clicking the code. The code is fairly simple:
n_inp = int(input("N: "))
num = []
for i in range(n_inp):
num.append(int(input("Number: " )))
print(num)
but for some reason the program just stopped after the loop is finished. It never prints the num, the program just closed. I tried using sleep command after the print(num) to see if it changes anything but it doesnt. Is there a problem with the code? Or should i just use some IDE to execute it? Thanks in advance.
edit: the code runs fine when executed from command prompt, i just wont run a code by double clicking again.
but i tried the same code in python3 it's working correctly...
num prints the output like....
N: 3
Number: 12
Number: 13
Number: 14
[12, 13, 14]
You are not able to see the output because it will close the terminal immediately after printing the output. You can just add sleep after the print(num) statement. Try the following code.
import time
n_inp = int(input("N: "))
num = []
for i in range(n_inp):
num.append(int(input("Number: " )))
print(num)
time.sleep(10)
or add one more input() after the print statement so that it will wait until you press any other button.
n_inp = int(input("N: "))
num = []
for i in range(n_inp):
num.append(int(input("Number: " )))
print(num)
input()

How to communicate two python files so one prints just before the other reads though the console (interactive)

what I want is something like this:
The first file just prints as soon as the 2nd has read
# a.py
print('pepe')
# wait till the other had read
print(23)
The second program uses data from the later
# b.py
name = input()
age = int(input())
print('Hi ' + name + ', you are ' str(age))
So I can see in the console:
Hi pepe, you are 23
I want to do this because, I waste a lot of time typing in the console. And I want to do it automatically.
Just in case, I browsed for this thing a long time, but no idea, that is why I decided to ask here.
A couple of different ways.
1) while True:
an infinite loop that requires you to use some sort of
while True:
cmd = input("type something")
if cmd == 'e':
do something
elif cmd == 'f':
do something else
else:
do that last thing
2) input("press enter to continue")
input('press enter to continue')
Hopefully that gets you what you need...
You can also learn more here: How do I make python to wait for a pressed key

Additional loop when stoping while loop

I've created a function using the carriage return from this and threading trick to exit loop from this. The function will keep counting until a keystroke is detected.
Everything works fine except an additional line is printed whenever I kill the loop.
For example in console:
Press anything to stop it: 6 #<------------- pressed enter
Press anything to stop it: 7 #<------------- additional line produced
import _thread
import time
import sys
def input_thread(list):
input()
list.append(None)
def do_stuff():
counter = 0
list = []
_thread.start_new_thread(input_thread,(list,))
while not list:
time.sleep(0.1)
sys.stdout.write("Press anything to stop it: %d \r" % (counter))
sys.stdout.flush()
counter += 1
How can I prevent extra line being printed? I suspect it has something to do with the while loop has to finish additional loop in order to exit. If the answer to this question is too obvious please let me know the keyword to search for.
Thanks a million!

Resources