-
Notifications
You must be signed in to change notification settings - Fork 0
/
app4.py
126 lines (106 loc) · 4.14 KB
/
app4.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
import tkinter as tk
from datetime import datetime, timedelta
import os
import pyautogui
# Nome do arquivo para gravar os comandos
command_file = "commands.txt"
# Variáveis globais
start_time = None
last_command_time = None
recording = False
# 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):
button.configure(bg="green")
log_command(command)
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()
# Criando a janela principal
root = tk.Tk()
root.title("Controle com Setas")
# Assegura que o arquivo existe
ensure_file_exists()
# Criando os botões
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=3, column=0, pady=10)
btn_pause.grid(row=3, column=1, pady=10)
btn_stop.grid(row=3, column=2, pady=10)
btn_exit.grid(row=4, 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=4, column=0, pady=10)
# Label para exibir o status
lbl_status = tk.Label(root, text="Parado")
lbl_status.grid(row=4, column=2, pady=10)
# Rodar a aplicação
root.mainloop()