-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
173 lines (127 loc) · 4.72 KB
/
GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import tkinter as tk
from os import system
from time import sleep, time
from threading import Thread
from importlib.util import find_spec
if not find_spec("pynput"): system("pip3 install pynput")
from pynput.mouse import Button, Controller
from pynput.keyboard import Key , Listener
# Shortcuts
QUIT_KEY = Key.scroll_lock
TOGGLE_KEY = Key.pause
INCREASE_KEY = Key.up
DECREASE_KEY = Key.down
BUTTON_LEFT = Key.left
BUTTON_RIGHT = Key.right
class Main:
def __init__(self):
self.root = tk.Tk()
self.cps = 1
self.alive = True
self.state = False
self.button = Button.left
self.stringVarInput = tk.StringVar()
self.stringVarToggle = tk.StringVar()
self.mouse = Controller()
self.setup()
self.run()
# run
def run(self):
self.lis = Listener(on_press=self.on_press )
self.loop = Thread (target =self.clickLoop)
self.lis .start()
self.loop.start()
self.root.mainloop()
def clickLoop(self):
while self.alive:
if self.state and self.cps > 0:
self.mouse.click(self.button)
self.sleep(1 / self.cps)
# helper
def sleep(self, sec):
while sec > 0:
tmp = 0.25 if sec > 0.25 else sec
sleep(tmp)
if not self.alive: return
sec -= tmp
# handler
def on_press(self, key):
if key == QUIT_KEY : self.quit()
elif key == TOGGLE_KEY : self.toggle()
elif key == INCREASE_KEY: self.increase()
elif key == DECREASE_KEY: self.decrease()
elif key == BUTTON_LEFT : self.switch_left()
elif key == BUTTON_RIGHT: self.switch_right()
def on_change(self, *_):
sv = self.stringVarInput.get()
if (sv != "" and not sv.isdecimal()):
return self.stringVarInput.set(str(self.cps))
self.cps = int(sv) if sv != "" else 0
# action
def increase(self):
self.cps += 1
self.stringVarInput.set(str(self.cps))
def decrease(self):
if self.cps > 1:
self.cps -= 1
self.stringVarInput.set(str(self.cps))
def quit(self):
self.alive = False
self.lis .stop()
self.loop.join()
self.root.destroy()
def toggle(self):
self.state = not self.state
self.stringVarToggle.set("Turn " +
("OFF" if self.state else "ON") +
f" ({TOGGLE_KEY.name})")
def switch_left(self):
self.button = Button.left
self.l.select()
self.r.selection_clear()
def switch_right(self):
self.button = Button.right
self.l.selection_clear()
self.r.select()
# setup GUI
def setup(self):
# setup var
self.stringVarInput.set("1")
self.stringVarInput.trace_add("write", self.on_change)
self.stringVarToggle.set("Turn ON" +
f" ({TOGGLE_KEY.name})")
# root
self.root.title ("Mouse Clicker")
self.root.protocol ("WM_DELETE_WINDOW", self.quit)
self.root.resizable(False, False)
# frames
topLevelFrame = tk.LabelFrame(self.root, text="Mouse Clicker")
topLevelFrame.pack(fill="both", expand=True, padx=5, pady=5)
buttonSelectFrame = tk.Frame(topLevelFrame)
buttonSelectFrame.pack(side=tk.TOP, fill=tk.X, padx=5)
cpsSelectFrame = tk.Frame(topLevelFrame)
cpsSelectFrame.pack(side=tk.TOP, fill=tk.X, padx=5)
# toggle button
tk.Button(
topLevelFrame,
command =self.toggle,
textvariable=self.stringVarToggle,
).pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
# button select
self.l = tk.Radiobutton(buttonSelectFrame, text="Left" , value=0, command=self.switch_left )
self.r = tk.Radiobutton(buttonSelectFrame, text="Right", value=1, command=self.switch_right)
self.l.grid(row=0, column=1, padx=23, sticky="nsew")
self.r.grid(row=0, column=2, padx=23, sticky="nsew")
self.l.select()
self.r.selection_clear()
# cps select
tk.Label(cpsSelectFrame, text="CPS:")\
.grid(row=0, column=0, rowspan=2, sticky="nsew")
i = tk.Entry(cpsSelectFrame, width=24, textvariable=self.stringVarInput)
i.grid(row=0, column=1, rowspan=2, sticky="nsew")
i.grid_columnconfigure(1, weight=2)
tk.Button(cpsSelectFrame, text="▲", font=("Arial", 5), command=self.increase)\
.grid(row=0, column=2, sticky="nsew")
tk.Button(cpsSelectFrame, text="▼", font=("Arial", 5), command=self.decrease)\
.grid(row=1, column=2, sticky="nsew")
if __name__ == "__main__": Main()