-2

I cant use bucles with tkinter cause it stops responding, i tried with while and for, i know there are more questions like this but i didnt understood, so guys please help me, i need ur help and yes im writting more because this isnt letting me post cause i have to add details

import time



spam_delay=0.001
spam_keys="cerdo"
spam_range=70
class spammer():
    def __init__(self,delay,keys):
        self.delay=spam_delay
        self.keys=spam_keys
        self.started=False

    **def start(self):
        self.started=True
        self.run()
    def run(self):
            if self.started:
                for i in range(spam_range):
                    time.sleep(spam_delay)
                    pyautogui.typewrite(spam_keys)
                    pyautogui.press("enter")
                    time.sleep(0.1)**
                    
    def stop(self):
        if self.started:
            self.started=False

spamerV2=spammer(spam_delay,spam_keys)

from tkinter import *

root=Tk()
root.title("Spammer_V2.0")
root.resizable(1,1)

Frame=Frame(root, width=400, height=600)
Frame.pack()

Label=Label(Frame, text="papu")
Label.grid(row=0, column=0,sticky="e", padx=10,pady=10)

Entry_Text=Entry(Frame)
Entry_Text.grid(row=0, column=1,sticky="e", padx=10,pady=10)
**btn=Button(root,text="papuu",command=spamerV2.start)
btn.pack()**
btn2=Button(root,text="apaga", command=spamerV2.stop)
btn2.pack()


root.mainloop()```

  • Could you clarify your question? What specifically isn't working, and what line is it happening on? SO's great for asking questions that might benefit other people with the same question, so if you edit yours to suit that purpose, it might be easier to answer. – TankorSmash Sep 22 '20 at 23:51
  • thanks for reading, the problem is in the function run() because i use a bucle so at the end of the code the variable "btn" looks for that function and when i press the button it says "Spammer is not responding" and i have to close the program and wait until windows find a solution – José Antonio Sep 23 '20 at 00:09

1 Answers1

0

The loop in the run method uses sleep so the application freezes until the loop is done (about 20 seconds). Use the after method instead for your 'loop'.

I also added a textbox for the spam text and delay

Here is the updated code:

import time

spam_delay=0.001
spam_keys="cerdo"
spam_range=70
class spammer():
    def __init__(self,delay,keys):
        self.delay=spam_delay
        self.keys=spam_keys
        self.started=False
        self.ctr = 0

    def start(self):
        self.started=True
        global spam_keys,spam_delay
        spam_keys = Entry_Text.get() or 'cerdo'  # spam text
        spam_delay = float(Entry_Delay.get())   # delay
        self.run()
    def run(self):
            if self.started:
                time.sleep(spam_delay)
                pyautogui.typewrite(spam_keys)
                pyautogui.press("enter")
                #time.sleep(0.1)
                self.ctr += 1
                if self.ctr < spam_range:
                   root.after(500, self.run)  # ms
                else:
                   self.ctr=0
                    
    def stop(self):
        if self.started:
            self.started=False
            self.ctr=0

spamerV2=spammer(spam_delay,spam_keys)

from tkinter import *
import pyautogui

root=Tk()
root.title("Spammer_V2.0")
root.resizable(1,1)

Frame=Frame(root, width=400, height=600)
Frame.pack()

Label=Label(Frame, text="papu")
Label.grid(row=0, column=0,sticky="e", padx=10,pady=10)

Entry_Text=Entry(Frame)
Entry_Text.grid(row=0, column=1,sticky="e", padx=10,pady=10)
Entry_Text.delete(0, END)
Entry_Text.insert(0, "cerdo")
Entry_Delay=Entry(Frame)
Entry_Delay.grid(row=0, column=2,sticky="e", padx=10,pady=10)
Entry_Delay.delete(0, END)
Entry_Delay.insert(0, "1")
btn=Button(root,text="papuu",command=spamerV2.start)
btn.pack()
btn2=Button(root,text="apaga", command=spamerV2.stop)
btn2.pack()


root.mainloop()
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • omg, thank you it works but i have another question, its kinda stupid but im new, how can i make a entryText that would storage the text the person wrote there in variables, for example one entry for spamdelay, another one for spam keys etc.. – José Antonio Sep 23 '20 at 00:23
  • Answer updated with new text box – Mike67 Sep 23 '20 at 00:42
  • THANKS!!! Just one more question, how can i make that in the delay entry box people can´t write letters, i want only numbers in that entry please! – José Antonio Sep 23 '20 at 00:54
  • That is somewhat complicated: https://stackoverflow.com/questions/8959815/restricting-the-value-in-tkinter-entry-widget. It may be easier to remove any letters when the user clicks the button (or show an error). – Mike67 Sep 23 '20 at 01:00
  • Ok bro, you are a genius – José Antonio Sep 23 '20 at 01:04
  • Bro, how can i make like another screen and put my autoclicker to have spammer and auto clicker in the same app – José Antonio Sep 23 '20 at 02:49