-
Notifications
You must be signed in to change notification settings - Fork 0
/
app7.py
168 lines (144 loc) · 5.73 KB
/
app7.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
import tkinter as tk
from datetime import datetime, timedelta
import os
import pyautogui
import pygetwindow as gw
# Nome do arquivo para gravar os comandos
command_file = "commands.txt"
# Variáveis globais
start_time = None
last_command_time = None
recording = False
target_window = None
# Função para verificar se o arquivo existe, se não, cria
def ensure_file_exists():
if not os.path.exists(command_file):
with open(command_file, "w") as file:
file.write("Arquivo de comandos criado.\n")
# Função para calcular o tempo decorrido desde o último comando
def get_time_since_last_command():
global last_command_time
if last_command_time:
elapsed = datetime.now() - last_command_time
hours, remainder = divmod(elapsed.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
return f"{int(hours)}h {int(minutes)}m {int(seconds)}s"
else:
return "0h 0m 0s"
# Função para gravar os comandos no arquivo
def log_command(command):
global last_command_time
if recording:
with open(command_file, "a") as file:
elapsed_time = get_time_since_last_command()
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(f"{timestamp} | Tempo desde último comando: {elapsed_time} | Comando: {command}\n")
last_command_time = datetime.now()
# Função para simular a tecla e registrar o comando
def on_button_click(button, command, key):
global target_window
button.configure(bg="green")
log_command(command)
if target_window:
# Ativar a janela alvo antes de enviar o comando de tecla
target_window.activate()
pyautogui.press(key)
button.after(200, lambda: button.configure(bg="SystemButtonFace"))
# Função para ler e reproduzir os comandos do arquivo
def replay_commands():
try:
with open(command_file, "r") as file:
for line in file:
print(line.strip())
except FileNotFoundError:
print("Nenhum comando gravado ainda.")
# Função para iniciar a gravação
def start_recording():
global recording, start_time, last_command_time
if not recording:
start_time = datetime.now()
last_command_time = start_time
recording = True
lbl_status.config(text="Gravando...")
# Função para pausar a gravação
def pause_recording():
global recording
if recording:
recording = False
lbl_status.config(text="Pausado")
# Função para parar a gravação
def stop_recording():
global recording, start_time, last_command_time
if recording:
recording = False
start_time = None
last_command_time = None
lbl_status.config(text="Parado")
# Função para sair do aplicativo
def exit_application():
root.destroy()
# Função para listar e atualizar janelas
def list_windows():
listbox_windows.delete(0, tk.END)
windows = gw.getWindowsWithTitle("")
for window in windows:
if window.visible and not window.isMinimized:
listbox_windows.insert(tk.END, window.title)
# Função para selecionar a janela alvo
def select_window():
global target_window
selection = listbox_windows.curselection()
if selection:
selected_title = listbox_windows.get(selection[0])
windows = gw.getWindowsWithTitle(selected_title)
if windows:
target_window = windows[0]
lbl_window_status.config(text=f"Janela alvo: {target_window.title}")
else:
lbl_window_status.config(text="Janela não encontrada")
else:
lbl_window_status.config(text="Selecione uma janela")
# Criando a janela principal
root = tk.Tk()
root.title("Controle com Setas")
# Assegura que o arquivo existe
ensure_file_exists()
# Criando os botões de direção
btn_up = tk.Button(root, text="↑", width=10, height=2, command=lambda: on_button_click(btn_up, "UP", "up"))
btn_down = tk.Button(root, text="↓", width=10, height=2, command=lambda: on_button_click(btn_down, "DOWN", "down"))
btn_left = tk.Button(root, text="←", width=10, height=2, command=lambda: on_button_click(btn_left, "LEFT", "left"))
btn_right = tk.Button(root, text="→", width=10, height=2, command=lambda: on_button_click(btn_right, "RIGHT", "right"))
# Posicionando os botões em forma de cruz
btn_up.grid(row=0, column=1)
btn_down.grid(row=2, column=1)
btn_left.grid(row=1, column=0)
btn_right.grid(row=1, column=2)
# Botões adicionais
btn_start = tk.Button(root, text="Ligar", command=start_recording)
btn_pause = tk.Button(root, text="Pausa", command=pause_recording)
btn_stop = tk.Button(root, text="Parar", command=stop_recording)
btn_exit = tk.Button(root, text="Sair", command=exit_application)
# Posicionando botões adicionais
btn_start.grid(row=4, column=0, pady=10)
btn_pause.grid(row=4, column=1, pady=10)
btn_stop.grid(row=4, column=2, pady=10)
btn_exit.grid(row=5, column=1, pady=10)
# Botão para reproduzir os comandos
btn_replay = tk.Button(root, text="Reproduzir Comandos", command=replay_commands)
btn_replay.grid(row=5, column=0, pady=10)
# Label para exibir o status
lbl_status = tk.Label(root, text="Parado")
lbl_status.grid(row=5, column=2, pady=10)
# Lista de janelas
listbox_windows = tk.Listbox(root, height=6, width=40)
listbox_windows.grid(row=0, column=3, rowspan=3, padx=10)
btn_refresh_windows = tk.Button(root, text="Atualizar Janelas", command=list_windows)
btn_refresh_windows.grid(row=3, column=3, pady=10)
btn_select_window = tk.Button(root, text="Selecionar Janela", command=select_window)
btn_select_window.grid(row=4, column=3, pady=10)
lbl_window_status = tk.Label(root, text="Nenhuma janela selecionada")
lbl_window_status.grid(row=5, column=3, pady=10)
# Inicializar a lista de janelas
list_windows()
# Rodar a aplicação
root.mainloop()