-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_gui.py
188 lines (153 loc) · 5.95 KB
/
auto_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Class to simplify the use of PyAutoGUI to multiple applications.
"""
import pyautogui
from time import sleep
from files_use import FileUse
from threading import Thread
class AutoGUI(object):
"""Class to simplify the use of PyAutoGUI to multiple applications."""
def __init__(self):
super(AutoGUI, self).__init__()
self.special_keys = [
"alt", "altleft", "altright", "enter", "ctrl", "ctrlleft", "ctrlright", "capslock",
"esc", "escape", "up", "down", "left", "right", "shift", "shiftleft", "shiftright",
"space", "tab"] + ["f" + str(i) for i in range(1, 25)]
self.init()
def init(self):
"""
Inits file control.
"""
self.files_control = FileUse()
def send_keys(self, keys_list=None):
"""
Press keys or write keys_list content.
- If it receives keys between '+', press modifiers and press
the last key in the list.
- If only receives modifiers, retains them.
"""
if keys_list:
for key in keys_list:
if "+" in key and len(key) > 1:
keys = key.split("+") # Divide la cadena para obtener las teclas
pyautogui.hotkey(*keys)
elif key in self.special_keys:
pyautogui.press(key)
else:
pyautogui.typewrite(key)
def mouse_move(self, move=None, rel=False, drag=False, function=None, iters=1):
"""
Moves the mouse.
- If receives a function, executes the function before each cycle.
- If rel, enables relative move.
- If drag, retains press the left button.
- If move is none, return mouse current position.
"""
if move == None:
return pyautogui.position()
if drag:
pyautogui.mouseDown()
if rel:
pyautogui.moveRel(move[0], move[1])
else:
pyautogui.moveTo(move[0], move[1])
pyautogui.mouseUp()
if function:
for i in range(iters):
x, y = function()
pyautogui.moveTo(x, y)
else:
if rel:
pyautogui.moveRel(move[0], move[1])
else:
pyautogui.moveTo(move[0], move[1])
def click(self, mode=None, clicks=1):
"""
Clicks in the position where the mouse is.
mode: Button to click.
clicks: number of clicks.
"""
if mode == None:
button = 'left'
else:
button = mode
pyautogui.click(button=button, clicks=clicks)
def scroll(self, amount, percent=False):
"""
Scrolls up or down.
- If percent, moves that precentage of screen.
"""
if percent:
amount = int((amount / 100) * self.get_screen_properties()[0])
pyautogui.scroll(amount)
def get_screen_properties(self):
"""
Returns screen propierties.
"""
return list(pyautogui.size())
def save_screenshot(self, filename=""):
"""
Get a snapshot and saves it in a file.
"""
file = self.files_control.create_file(filename, extension = "png")
return pyautogui.screenshot(file.name)
def get_on_screen(self, image, confidence=0.5):
"""
Search for a image on screen.
- If OpenCV is in use, it enables an advance searching.
- confidence parameter defines precision of searching. Needs OpenCV.
If no image was found, returns None.
"""
try:
if confidence < 0.9:
grayscale = True
else:
grayscale = False
coordenadas = pyautogui.locateCenterOnScreen(image, grayscale=grayscale, confidence=confidence)
except pyautogui.ImageNotFoundException:
coordenadas = None
except NotImplementedError:
coordenadas = pyautogui.locateCenterOnScreen(image, grayscale=grayscale)
return coordenadas
def gui_functions(self, kind, options):
"""
Function that decides which interface use and generates it.
options, 0: title
options, 1: question text
options, 2: button text
options, 3: function to call with selection
"""
if kind == "alert":
result = pyautogui.alert(title=options[0], text=options[1], button=options[2])
elif kind == "confirm":
result = pyautogui.confirm(title=options[0], text=options[1], buttons=options[2])
elif kind == "prompt":
result = pyautogui.prompt(title=options[0], text=options[1], default=options[2])
elif kind == "password":
result = pyautogui.password(title=options[0], text=options[1], default=options[2], mask='*')
options[3](result)
# Function called with the answer
def gui(self, kind, options, asyncronous=False):
"""
Includes all posible GUI that pyautogui offers in a function.
- type defines the type of the GUI.
- options is a list with the needed parameters. If it receives less
than needed, returns None. If it receives more, use only needed.
"""
if len(options) != 4:
return None
# PyAutoGUI function uses three parameters, the last one is
# the function which receives the result.
if asyncronous:
self.hilo = Thread(target=self.gui_functions, args=(kind, options,))
self.hilo.start()
else:
self.gui_functions(kind, options)
def main():
autogui = AutoGUI()
autogui.mouse_move((500, 500))
autogui.click(mode="right")
if __name__ == "__main__":
main()