I wrote this little program which spams in messenger for me. But I can't make it to stop if I want to. I tried to put 'try / except: KeyboardInterrupt' in the loop but it did not help.
I need python to somehow check if I press some key and if it's pressed break the loop. Also I know there is a after()
method in Tkinter, and I should use it instead of time.sleep
, but my attempt failed. Here is my code:
from tkinter import *
import time
import pyautogui
root = Tk()
root.title('Typer')
text_field = Text(root, height=20, width=40)
button = Button(text='----Start----')
def typer(event):
text = text_field.get("1.0",END)
time.sleep(5)
for i in text.split(' '):
pyautogui.write(i)
pyautogui.press('enter')
button.bind('<Button-1>', typer)
text_field.pack()
button.pack()
root.mainloop()
Update:
I manage to change time.sleep()
to after()
by doing this:
def typer(event):
def innertyper():
for i in text.split(' '):
pyautogui.write(i)
pyautogui.press('enter')
text = text_field.get("1.0",END)
root.after(5000, innertyper)
But I still can't break the for loop