#๐Ÿ”’ running another loop with tkinter

22 messages ยท Page 1 of 1 (latest)

high wind
#

tried making an autoclicker, but cant figure out how to run another while loop with the main loop of tkinter, and threading didnt work

import tkinter
import pyautogui
import threading


clicking_status = False
def check_key(event):
    if event.keysym != "F2":
        return
    if activate_clicker['state'] == 'active':
        activate()
    else:
        deactivate()

def autoclicker(clicks: int):
    global clicking_status
    clicking_status = True
    while clicking_status:
        mainScreen.after(int(1/clicks * 1000), pyautogui.click())

def activate():
    activate_clicker['state'] = "disabled"
    deactivate_clicker['state'] = 'active'
    value = ClicksPerSecond.get()
    if value.isdigit():
        autoclicker(int(value))
    else:
        return

def deactivate():
    global clicking_status
    activate_clicker['state'] = "active"
    deactivate_clicker['state'] = 'disabled'
    value = ClicksPerSecond.get()
    if value.isdigit():
        clicking_status = False
    else:
        return


mainScreen = tkinter.Tk()
mainScreen.attributes('-topmost', True)
mainScreen.bind("<KeyRelease>", check_key)
mainScreen.title("AutoClicker but worse")
mainScreen.minsize(400, 350)
descriptor = tkinter.Label(mainScreen, text="Clicks Per Second")
descriptor.place(x=5, y=0)
ClicksPerSecond = tkinter.Entry(mainScreen, width=20)
ClicksPerSecond.place(x=5, y=20)
activate_clicker = tkinter.Button(mainScreen, height=1, width=10, text="Activate", command=activate)
activate_clicker.place(x=5, y=320)
deactivate_clicker = tkinter.Button(mainScreen, height=1, width=10, text="De-Activate", command=deactivate,
                                    state=tkinter.DISABLED)
deactivate_clicker.place(x=105, y=320)
mainScreen.mainloop()```
tacit gyroBOT
#

@high wind

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vocal pumice
#

Where do you use threading?

#

Also, the callback to after needs to be passed uncalled. You're calling click then passing the return to after.

vocal pumice
#

Functions are called using () , so to pass the function uncalled, you just remove the parenthesis.

#

Or do lambda: pyautogui.click()

high wind
#

oh bruh

#

the tkinter tab just freezes now after I activate it, Im not sure what the problem is though

vocal pumice
#

Well, you're spamming the UI thread now. What you had before made after useless, which is why I suggested fixing it, but the fixed code is also problematic.

high wind
#

is there another way to do clicking with the tkinter tab running simultaneously?

vocal pumice
#

Where is the threading code? That would work if done properly.

high wind
vocal pumice
#

You can't run Tkinter code in another thread, but I'd need to see the attempt to know what the problem is. Running pyguiauto.click() is another thread should be fine.

high wind
#

oh yea, think my earlier problem was running the mainloop in another thread in it self

#
import tkinter
import pyautogui
import threading


clicking_status = False
def check_key(event):
    if event.keysym != "F2":
        return
    if activate_clicker['state'] == 'active':
        activate()
    else:
        deactivate()

def autoclicker(clicks: int):
    global clicking_status
    clicking_status = True
    while clicking_status:
        pyautogui.click()

def activate():
    activate_clicker['state'] = "disabled"
    deactivate_clicker['state'] = 'active'
    value = ClicksPerSecond.get()
    if value.isdigit():
        thread = threading.Thread(target=autoclicker, args=(value, ))
        thread.start()
    else:
        return

def deactivate():
    global clicking_status
    activate_clicker['state'] = "active"
    deactivate_clicker['state'] = 'disabled'
    value = ClicksPerSecond.get()
    if value.isdigit():
        clicking_status = False
    else:
        return


mainScreen = tkinter.Tk()
mainScreen.attributes('-topmost', True)
mainScreen.bind("<KeyRelease>", check_key)
mainScreen.title("AutoClicker but worse")
mainScreen.minsize(400, 350)
descriptor = tkinter.Label(mainScreen, text="Clicks Per Second")
descriptor.place(x=5, y=0)
ClicksPerSecond = tkinter.Entry(mainScreen, width=20)
ClicksPerSecond.place(x=5, y=20)
activate_clicker = tkinter.Button(mainScreen, height=1, width=10, text="Activate", command=activate)
activate_clicker.place(x=5, y=320)
deactivate_clicker = tkinter.Button(mainScreen, height=1, width=10, text="De-Activate", command=deactivate,
                                    state=tkinter.DISABLED)
deactivate_clicker.place(x=105, y=320)
mainScreen.mainloop()```

Also have another problem where clicking the F2 button doesnt work sometimes
vocal pumice
#

mainloop is for tkinter. If all you want to do is run a clicking loop, that's not related to tkinter, so you wouldn't start a new event loop for tkinter.

#

Just put a loop in the threading function and have it call click.

#

Ya, like you already have there. Does that cause an error?

#

That loop may still cause problems, since clicking as fast as your machine will allow will force it to do a lot of constant work, but it should work without error.

tacit gyroBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.