5
\$\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()
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 26, 2014 at 3:29
\$\endgroup\$
2
  • \$\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\$ Commented Dec 26, 2014 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\$ Commented Dec 26, 2014 at 17:32

2 Answers 2

2
\$\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).

answered Jan 5, 2015 at 22:43
\$\endgroup\$
1
  • 1
    \$\begingroup\$ f.read() == "True" is already a boolean operation. No need to wrap it with the ternary if/else. \$\endgroup\$ Commented Jan 5, 2015 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.