4
\$\begingroup\$

I made my first program that I might actually use and I was wondering if anyone had some tips on how I could improve it.

My code is an auto-clicker that uses Python3 ctypes for the clicking and Tkinter for the GUI. I made this in 2 hours so there might be a lot of optimization I could do.

from tkinter import *
from tkinter import ttk
import ctypes
import time

mouse = ctypes.windll.user32
state = False

def StateFalse():
     global state
     global x
     if state == True:
          state = False
          x = 0

def StateTrue():
     global state
     global x
     if state == True:
          state = True
     else:
          x = 0
          state = True

root = Tk()
root.title("AutoClicker")
root.resizable(width=FALSE, height=FALSE)

amount = StringVar()
speed = StringVar()

amount.set(0)
speed.set(100)
mainframe = ttk.Frame(root)
mainframe.grid(padx=5, pady=5)

amountLabel = ttk.Label(mainframe, text="Number of Clicks\n(set to 0 for infinite)").grid(column=1, row=1, sticky=W)
speedLabel = ttk.Label(mainframe, text="Click interval\n(In milliseconds)").grid(column=1, row=2, sticky=W)
amountEntry = ttk.Entry(mainframe, textvariable=amount, width=5).grid(column=2, row=1)
speedEntry = ttk.Entry(mainframe, textvariable=speed, width=5).grid(column=2, row=2)
startButton = ttk.Button(mainframe, text="Start", width=10, command=StateTrue).grid(column=1, row=3,columnspan=2,sticky=W)
stopButton = ttk.Button(mainframe, text="Stop", width=10, command=StateFalse).grid(column=2, row=3, columnspan=2,sticky=E)

mainframe.bind("<F1>",StateTrue())
mainframe.bind("<F3>",StateFalse())

timekeeper = 0
x = 0

while True:
     timekeeper += 1
     if state == True:
          Amount = int(amount.get())
          Speed = int(speed.get())
          print(state)
          if Amount == 0:
               time.sleep(Speed / 1000)
               mouse.mouse_event(2, 0, 0, 0, 0)  # left mouse button down
               mouse.mouse_event(4, 0, 0, 0, 0)  # left mouse button up
               x += 1
               print("Clicked %s Times" % (x))

          elif x < Amount and state == True:
               time.sleep(Speed / 1000)
               mouse.mouse_event(2, 0, 0, 0, 0)  # left mouse button down
               mouse.mouse_event(4, 0, 0, 0, 0)  # left mouse button up
               x += 1
               print("Clicked %s Times" % (x))
               if x == Amount:
                    state = False
     root.update()
\$\endgroup\$

migrated from stackoverflow.com Jan 5 '15 at 14:11

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Use the .after method to schedule event later. There are several examples in answers. widget.event_generate can be used to simulate mouse clicks on a widget. \$\endgroup\$ – Terry Jan Reedy Dec 26 '14 at 10:18
  • \$\begingroup\$ Set DWORD = ctypes.c_ulong; ULONG_PTR = ctypes.c_ulong if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong) else ctypes.c_ulonglong; user32 = ctypes.windll.user32; user32.mouse_event.argtypes = (DWORD, DWORD, DWORD, DWORD, ULONG_PTR); user32.mouse_event.restype = None. \$\endgroup\$ – Eryk Sun Dec 26 '14 at 17:32
1
\$\begingroup\$

You are using global variables and connecting them with the functions StateFalse and StateTrue. It would be better to read and write the configuration options to a file.

def set_option(value):
    with open(CONFIG_FILE,"w+") as f:
        f.write(value)

def get_option():
    with open(CONFIG_FILE) as f:
        return f.read() == "True"

Where CONFIG_FILE is a filename of your choice that you choose to store the "state" (maybe you should find a better name).

\$\endgroup\$
  • 1
    \$\begingroup\$ f.read() == "True" is already a boolean operation. No need to wrap it with the ternary if/else. \$\endgroup\$ – unholysampler Jan 5 '15 at 23:34
0
\$\begingroup\$

Your code is hard to read and follow. You can start cleaning it by:

  1. Applying tkinter best practices.
  2. Complying with PEP8.

In 2 different functions, you have written:

if state == True:
   # ...

In Python, you can simply write:

if state:
   # ...
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy