0

i'm a beginner in python, only having finished a course on it and currently working through a book.

A fairly simple project (and one of the few actually useful things i could think of) is an anit-AFK tool. I started googleing and found a few elements that i fried to combine. So i "frankensteined" a script together by stitching different elements and my first test seem to work kinda fine, generally sort of doing what i want it to.

However i have a few problems: 1: tkinter GUI doesn't really work (i'm running the code directly from PyCharm); a window opens but it completely lacks the buttons or anything really. It's a blank window, that seems to have the dimension i gave it, but lacking everything else.

2: I don't know how to make it press random keys, from a set of pre-defined keys and key combinations. My idea was assigning each key to a number, then having a random number generator pick a number, assigning it to a variable. All of that in a loop so that each time another loop decides it's time to press a key, the key (combination') is a new random one.

Now that i've explained the general idea, here's the code i have until now:

import pyautogui
from random import randint
from time import sleep
from tkinter import *

running = True

key = ["w", "a", "s", "d"] # this is the "base" keys
key_2 = [key[randint(0,3)], key[randint(0,3)] + "shift", "space"] # this is for combinations

def anti_afk():
    while running:
        pyautogui.press(key_2[randint(0,2)])
        print("Test") # to easily test if the sleep is working working
        sleep(randint(5,30))

def start():
    running = True

def stop():
    running = False

root = Tk()
root.title("Anti AFK Tool")
root.geometry ("500x500")

app = Frame(root)
app.grid

start = Button(app, text="Start anti-AFK", command=start)
stop = Button(app, text="Stop preventing AFK", command=stop)

start.grid()
stop.grid()

root.after(1000, anti_afk)
root.mainloop()

As i said above i'm a beginner and pretty much everything regarding tkinter and pyautogui isn't written by me but taken from THIS and THIS post right here. Also this is the very first time i use tkinter

So, when running it with only one key (W key in my case) it generally seems to work kinda fine. The GUI doesn't work beyond what i described above. I'm programming on my MacBook and get the "spinning ball of death" in the application window. Generally i want the program do be running mainly on windows.

So the problem is that the random key selections seems to be not working like i want it to. Especially the (in this case) shift+key doesn't. Also the GUI of the program doesn't work.

Another idea i had was defining a function for each action seperately and then randomly calling that function. But the problem of how to call / use random functions / list elements is basically the same and the list solution seems much more flexible with less code.

Can you help me with this?

Zabric
  • 1
  • 1

0 Answers0