pyautogui.click() doesn't work as expected - python

Actually, I have written a code where I've to lunch the application such that I've to click the on-screen keyboard using this pyautogui.click(). But it is not working on on-screen keyboard. I'll be pleased to have your precious opinion. Thanks in advance.
import os
import pyautogui as pg
import time
x= 195
y=505
secret="secretpassword"
command = "application"
os.system(command)
pg.click(x, y)
pg.typewrite(secret)
pg.typewrite(["enter"])
If the application is already lunched this is working but i want to lunch it with os.system(command)
and after that enter my password and access to the application.
Am I doing something wrong ?

I changed
os.system(command)
with
subprocess.Popen(command)
Now it's working
subprocess.Popen() is strict super-set of os.system().

os.system() will block and wait for the application to exit.
This means that your click, in fact, will not execute until the opened appication closes.
To verify this, you can open a (python) shell and run following code:
import os
import pyautogui
def test():
os.system('<something simple opening a window>')
pyautogui.typewrite("I'm in the shell again!")
test()
To run your script as you want, use os.popen, or, even better, subprocess.Popen. These will run the command without blocking.
If you do this, keep in mind your application will have startup time, so you will want to wait some time after the call as noted in the comments under your question.

Related

Show command prompt in tkinter window

How do I make a program where I can run a piece of code, then show the results? So if I make my program run python --version it should print something like Python 3.8.3 (depends on what version you are on), but you get the point
PS: I know this has been posted before, but they don't work for me :(
Thanks!!
So here I made a very simple version of what You may want (type in python --version to try out):
from tkinter import Tk, Text
import subprocess
def run(event):
command = cmd.get('1.0', 'end').split('\n')[-2]
if command == 'exit':
exit()
cmd.insert('end', f'\n{subprocess.getoutput(command)}')
root = Tk()
cmd = Text(root)
cmd.pack()
cmd.bind('<Return>', run)
root.mainloop()
the subprocess.getoutput() gets the output the cmd would give if the given command was used
EDIT (moved comment here):
there are some limitations however for example running pause will just crash tkinter and the output will be given only after command has finished running for example if You tracert google.com it may take a while and during that the window will be unresponsive until it completes the process and then puts it all out (maybe for that it is possible to use threads to not make the window unresponsive at least)
EDIT (28.07.2021.):
Probably better to use subprocess.Popen and stream data from there

Create an App that cmd ''ipconfig/flushdns'' command each certain time using Python?

I'd like to create short app that runs with windows start and execute flashdns for example every 1h.
Maybe compile to into exe file.
I know that start of this will be:
import os
os.system('ipconfig/flushdns')
but i didn't find answer how this function will look like and how to compile it and run it with windows.
This guide should get you covered, it shows how you can run and execute commands in cmd: https://datatofish.com/command-prompt-python/
To run your command on a schedule, you can use an infinite while loop and time.sleep, which takes a number of seconds to delay thread execution by:
import time
import os
while True:
os.system("ipconfig /flushdns")
time.sleep(3600)
If you are doing other things in this program, you may want to run this loop in a thread.
To "compile" your program into a Windows executable file, you can use py2exe or cx_Freeze (just some examples, there are certainly more tools available that have this functionality).
Big Thanks to:
https://stackoverflow.com/users/9359583/kremklatt
I used links:
How to start a python file while Windows starts?
https://datatofish.com/command-prompt-python/
And Final Script:
import os
import threading
def flushdns():
threading.Timer(60.0, flushdns).start() # repeat every 60 seconds
os.system('cmd /k "ipconfig/flushdns"')
flushdns()
This Community is Epic xD
https://github.com/perdubaro/DnsFlusher
any feedback welcome :)

Python pywinauto PuTTy how to wait till the task is over

I use Application from pywinauto.application
After logging in i want it to execute commads like :
putty.type_keys("ls")
putty.type_keys("{ENTER}")
To execute next command i need to wait for this one to end. Instead of typing something like :
time.sleep(5)
I need the program to know when the command is done and ready for next command, not to wait X seconds and hope the running task will be over untill that(for example downloadign a file). I looked up into "wait()", but didn't find anything useful. Any help?
You don’t need pywinauto for executing console commands by ssh! Just do something like this:
import subprocess
output = subprocess.check_output(“ssh user:password#hostname ls -l /home”)
for line in output.split(“\n”):
subpath = “ “.join(line.split(“ “)[1:])
print(subpath)

Python Pinging -t

I have a batch program that simply pings in loop with 'ping adress -t'. Adress being whatever I'm trying to ping at the time.
I'd like to do something similiar with python, but without the popup of a command prompt window and thus I'd like to avoid anything that would do this. I want a way to print it ONLY to the python window, so I can use it in my Tkinter program.
This is what I would originially thought would work, and it does, but I want the output to be in the python window, not in the command prompt.
import subprocess
subprocess.call(["ping", "google.com", "-t"])
What about:
import subprocess
subprocess.call(["ping", "-t"])

Issues with repeatedly executing .pyc script

I am trying to make an script that will every second read string from a file, and execute it.
executer.pyc:
import os, time
f = open("/root/codename/execute","a")
f.write("")
f.close()
cmd=open('/root/codename/execute', 'r').read()
if not cmd=="":
os.system(cmd)
os.system("rm /root/codename/execute")
time.sleep(1)
os.system("python executer.pyc")
Problem is, that it constantly f's up whole ps -aux and other similiar commands.
How can i make, that it will kill itself and then launch itself again? My idea, is a parent script that will launch executer.pyc everytime that script closes itself. But how can i make it, that it will not have effect like executer.pyc? I know this whole system how it works is kinda bad, but i just need it this way (reading from file "execute"). Please help!
Thanks in advance!
instead of
os.system("python executer.pyc")
you can use execfile()
It will be much easier to let this script run continuously. Look at How to use a timer to run forever? This will show you how you can repeatedly execute the same command.

Categories