1+ import pathlib
2+ import pygubu
3+ from rutina .accion import Accion
4+ from utils .helpers import select_area
5+ from tkinter import Toplevel , Label , Entry , Button
6+
7+ PROJECT_PATH = pathlib .Path (__file__ ).parent
8+ PROJECT_UI = PROJECT_PATH / "main.ui"
9+
10+ class IziBot :
11+ def __init__ (self , master = None ):
12+ self .builder = builder = pygubu .Builder ()
13+ builder .add_resource_path (PROJECT_PATH )
14+ builder .add_from_file (PROJECT_UI )
15+ self .mainwindow = builder .get_object ('main' , master )
16+ builder .connect_callbacks (self )
17+
18+ # Obtener referencia al Treeview de Rutina
19+ self .treeview_rutina = self .builder .get_object ('tvRutina' )
20+ # Configurar columnas del Treeview
21+ self .treeview_rutina ['columns' ] = ('#1' , '#2' )
22+ self .treeview_rutina .heading ('#0' , text = 'ID' )
23+ self .treeview_rutina .column ('#0' , width = 50 )
24+ self .treeview_rutina .heading ('#1' , text = 'Acción' )
25+ self .treeview_rutina .heading ('#2' , text = 'Cordenadas/Texto/Tiempo' )
26+ # Lista que contiene las acciones de la rutina (simulación)
27+ self .listaAccionesUsuario = []
28+
29+ # Obtener referencia de boton Clic
30+ self .btnAddClic = self .builder .get_object ('btnClic' )
31+
32+ # Variable para guardar area seleccionada
33+ self .temp_area = None
34+ # Variable para guardar texto leído
35+ self .texto_leido = None
36+
37+ def run (self ):
38+ self .mainwindow .mainloop ()
39+
40+ def agregar_accion (self , tipo_accion , * args ):
41+ accion = Accion (tipo_accion , * args )
42+
43+ if len (args ) == 1 :
44+ args = args [0 ] # Si solo hay un valor en args, lo desempaquetamos
45+
46+ self .listaAccionesUsuario .append (accion )
47+
48+ # Insertar nueva acción en el Treeview
49+ idx = len (self .listaAccionesUsuario )
50+ self .treeview_rutina .insert ('' , 'end' , text = str (idx ), values = (tipo_accion , str (args )))
51+
52+ def add_clic (self ):
53+ self .temp_area = select_area (self .mainwindow )
54+ x1 , y1 , x2 , y2 = self .temp_area
55+ self .agregar_accion ("clicEnPantalla" , x1 , y1 )
56+
57+ def add_esperar (self ):
58+ # Crear un cuadro de diálogo para ingresar el tiempo de espera
59+ self .dialog = Toplevel (self .mainwindow )
60+ self .dialog .title ("Ingrese el tiempo de espera" )
61+
62+ Label (self .dialog , text = "Ingrese el tiempo en segundos:" ).pack (pady = 10 )
63+
64+ # Entrada para el tiempo
65+ self .entry_tiempo = Entry (self .dialog )
66+ self .entry_tiempo .pack (pady = 5 )
67+
68+ # Botón para confirmar la entrada
69+ Button (self .dialog , text = "Aceptar" , command = self .confirmar_espera ).pack (pady = 10 )
70+
71+ def confirmar_espera (self ):
72+ # Obtener el valor ingresado
73+ tiempo = self .entry_tiempo .get ()
74+
75+ # Verificar que el valor sea un número válido
76+ try :
77+ tiempo = int (tiempo )
78+ if tiempo < 0 :
79+ raise ValueError ("El tiempo no puede ser negativo." )
80+ except ValueError :
81+ print ("Por favor, ingrese un número válido." )
82+ return
83+
84+ # Cerrar el cuadro de diálogo
85+ self .dialog .destroy ()
86+
87+ # Agregar la acción de esperar con el tiempo ingresado
88+ self .agregar_accion ("esperar" , tiempo )
89+
90+ def add_escribir (self ):
91+ # Crear ventana emergente para que el usuario ingrese el texto
92+ self .dialog = Toplevel (self .mainwindow )
93+ self .dialog .title ("Ingresar texto para escribir" )
94+
95+ # Etiqueta
96+ label = Label (self .dialog , text = "Texto a escribir:" )
97+ label .pack (padx = 10 , pady = 10 )
98+
99+ # Campo de entrada de texto
100+ self .entry_texto = Entry (self .dialog )
101+ self .entry_texto .pack (padx = 10 , pady = 10 )
102+
103+ # Botón para confirmar
104+ boton_confirmar = Button (self .dialog , text = "Confirmar" , command = self .confirmar_escribir )
105+ boton_confirmar .pack (padx = 10 , pady = 10 )
106+
107+ def confirmar_escribir (self ):
108+ # Obtener el texto ingresado
109+ texto = self .entry_texto .get ()
110+
111+ # Verificar que se haya ingresado algún texto
112+ if not texto :
113+ print ("Por favor, ingrese un texto válido." )
114+ return
115+
116+ # Cerrar la ventana emergente
117+ self .dialog .destroy ()
118+
119+ # Agregar la acción de 'escribir' con el texto ingresado
120+ self .agregar_accion ("escribir" , texto )
121+
122+ def add_leer (self ):
123+ self .temp_area = select_area (self .mainwindow )
124+ self .agregar_accion ("leerTextoEnPantalla" , self .temp_area )
125+
126+ def iniciar_rutina (self ):
127+ for accion in self .listaAccionesUsuario :
128+ accion .ejecutar ()
129+
130+ def eliminar_accion (self ):
131+ # Obtener el elemento seleccionado en el Treeview
132+ selected_item = self .treeview_rutina .selection ()
133+
134+ if selected_item :
135+ # Obtener el índice del elemento seleccionado
136+ item_index = int (self .treeview_rutina .item (selected_item , "text" )) - 1 # Restar 1 porque el índice es 1-based
137+
138+ # Eliminar el elemento de listaAccionesUsuario
139+ if 0 <= item_index < len (self .listaAccionesUsuario ):
140+ del self .listaAccionesUsuario [item_index ]
141+
142+ # Eliminar el elemento del Treeview
143+ self .treeview_rutina .delete (selected_item )
144+
145+ # Actualizar el Treeview para reflejar los nuevos índices y argumentos correctamente
146+ self .actualizar_treeview ()
147+ else :
148+ print ("No se ha seleccionado ninguna acción." )
149+
150+ def actualizar_treeview (self ):
151+ # Eliminar todos los elementos del Treeview
152+ for item in self .treeview_rutina .get_children ():
153+ self .treeview_rutina .delete (item )
154+
155+ # Volver a agregar todos los elementos de listaAccionesUsuario
156+ for idx , accion in enumerate (self .listaAccionesUsuario , start = 1 ):
157+ # Si los argumentos tienen un solo elemento, lo mostramos sin paréntesis
158+ args = accion .args if len (accion .args ) > 1 else accion .args [0 ] if accion .args else ''
159+
160+ # Insertar de nuevo en el Treeview
161+ self .treeview_rutina .insert ('' , 'end' , text = str (idx ), values = (accion .tipo_accion , str (args )))
162+
163+ if __name__ == "__main__" :
164+ app = IziBot ()
165+ app .run ()
0 commit comments