1
I want to automate a task, as the software has no api that allows this, I will control the mouse of the OS and click using Phyton.
I intend to create validations with images to ensure that I am on the right screen and clicking on the right place, even so I would like extra protection, which would stop the execution by pressing P on the keyboard and continue by clicking U.
- My question is, if you have how to do this without having to repeat the check to each line of the program, wait for the click and stop the execution when receiving the user info.
I’m new to Phyton, first try.
Edited
Adjusting the question to answer some questions posed by @jsbueno:
- I’m using Windows 10.
- It is a program with only lines of code, edited in VSCODE.
- I’m importing pyautogui (not yet actually), curses and threading.
Now in the morning I’ve been studying about this and I’ve already got some results, it’s stopping and continuing correctly, but when giving the stop() to kill the thread, it doesn’t release the terminal, it gets stuck without asking for a key or anything, follows the code:
from threading import Thread
from threading import Event
from time import sleep
import curses
class xx(Thread):
_stop = 0
def __init__(self):
self._active = Event()
self._active.set()
Thread.__init__(self)
def run(self):
global x
global f
y = 1
while True:
if self._stop != 0:
return
self._active.wait()
x += 1
y += 1
sleep(1)
f = open("output.txt", "a+")
f.write("x: " + str(x) + " - y: " + str(y) + "\n")
f.close()
if x == 10:
return
def pause(self):
self._active.clear()
def play(self):
self._active.set()
def stop(self):
self._stop = 1
x = 0
f = open("output.txt", "w+")
f.write("Inicio\n")
f.close()
def main(win):
f = open("output.txt", "a+")
f.write("Main Start\n")
f.close()
instx = xx()
instx.start()
key = ""
while True:
win.clear()
win.addstr("Detected key:")
win.addstr(str(key))
if key == 't':
instx.stop()
return
elif key == 'p':
instx.pause()
elif key == 'u':
instx.play()
key = win.getkey()
instx.join()
curses.wrapper(main)
I’m having trouble debugging the program, what you Python developers use for this ?
And if you have any book tips or courses to start in the language I am grateful.
To generate the above code, I took information from the following sources:
Thread work
* How to create a pause method and one that resumes a Thread?
* https://stackoverflow.com/questions/15063963/python-is-thread-still-running
* https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/
Keypad
* https://stackoverflow.com/questions/24072790/detect-key-press-in-python
Working with files ( for debug )
* https://www.guru99.com/reading-and-writing-files-in-python.html
Concatenate string and int
* https://www.journaldev.com/23642/python-concatenate-string-and-int
if you are using "threading", nothing will be able to debug the program. Without threads, it is only for a "breakpoint()" and follow the execution by the terminal.
– jsbueno