2012-01-31 08:27 @394 |
|
|
princi
Perlero Nuevo
|
Registrado: 2012-01-31 08:00 @375 Mensajes: 7
|
|
|
bind en Tk RESUELTO
|
Hola, soy bastante nueva en Perl. Mi problema es que creé una tabla y necesito que por cada fila me genere un archivo txt, pero no conozco, ni encontré el funcionamiento de bind... (acá está la tabla) Using perl Syntax Highlighting $mw1->Label (-text => "Informe", -font => 'ARIAL',-foreground=>'DARK BLUE') -> pack();
my $frame = $mw1->Frame()->pack();
my $T = $frame-> Table(-columns => 8, -rows => 11,-fixedrows => 1,-scrollbars => 'oe', -relief => 'raised');
$tmp_label = $frame->Label(-text => $i, -padx => 2,-anchor => 'center', -background => 'white', -relief => "groove");
$T->put($i,1,$tmp_label);
$tmp_label = $frame->Label(-text => $A, -padx => 2,-anchor => 'center', -background => 'white', -relief => "groove");
$T->put($i,2,$tmp_label);
$tmp_label = $frame->Label(-text => "txt" , -padx => 2,-anchor => 'center', -background => 'white', -relief => "groove");
$T->put($i,8,$tmp_label);
$tmp_label->bind('<Button-1>',\&file );
¡Muchas Gracias! Espero haber sido explicita en mi duda... ¡Saludos!
|
2012-01-31 19:48 @867 |
|
|
princi
Perlero Nuevo
|
Registrado: 2012-01-31 08:00 @375 Mensajes: 7
|
|
|
Re: bind en Tk
|
Así es, es entorno gráfico... el código completo sería este: Using perl Syntax Highlighting my $mw1 = MainWindow->new;
$mw1->geometry("600x350");
$mw1->resizable( 1, 1 );
$mw1->title("Ultimos 10 ");
my $fichero = "C:\\DoNotCopyToEB\\old.dat";
open FICHERO, "$fichero";
my @linea = <FICHERO>;
$mw1->Label( -text => "Informe", -font => 'ARIAL', -foreground => 'DARK BLUE' )->pack();
my $frame = $mw1->Frame()->pack();
my $T = $frame->Table( -columns => 8, -rows => 11, -fixedrows => 1, -scrollbars => 'oe', -relief => 'raised' );
#* * * * * * * ENCABEZADOS * * * * * *
my $tmp_label = $frame->Label( -text => " N° ", -justify => 'center', -width => 3, -relief => 'raised' );
$T->put( 0, 1, $tmp_label );
$tmp_label = $frame->Label( -text => "FECHA ", -width => 12, -relief => 'raised' );
$T->put( 0, 2, $tmp_label );
$tmp_label = $frame->Label( -text => "HORA ", -width => 10, -relief => 'raised' );
$T->put( 0, 3, $tmp_label );
$tmp_label = $frame->Label( -text => "L ", -width => 10, -relief => 'raised' );
$T->put( 0, 4, $tmp_label );
$tmp_label = $frame->Label( -text => "Lo ", -width => 10, -relief => 'raised' );
$T->put( 0, 5, $tmp_label );
$tmp_label = $frame->Label( -text => "PR ", -width => 15, -relief => 'raised' );
$T->put( 0, 6, $tmp_label );
$tmp_label = $frame->Label( -text => "MA ", -width => 10, -relief => 'raised' );
$T->put( 0, 7, $tmp_label );
$tmp_label = $frame->Label( -text => "file ", -width => 8, -relief => 'raised' );
$T->put( 0, 8, $tmp_label );
#* * * * * * * * LLENADO DE TABLA* * * * * * * * * *
for ( $i = 1; $i < 11; $i++ ) {
my @datos = split( /\s+/, $linea[ $i - 1 ] ); #/\s/ es un espacio en blanco (space)
$lat[$cont] = sprintf( "%5.2f", $datos[1] );
$lon[$cont] = sprintf( "%5.2f", $datos[2] );
$dep[$cont] = sprintf( "%3.1f", $datos[6] );
$sta[$cont] = $datos[9];
$m1mag[$cont] = sprintf( "%4.2f", $datos[3] );
$mag{Ms} = $datos[16];
#print "Ms $datos[16] \n";
$mag{Mw} = $datos[20];
#print "Mw $datos[20] \n";
$mag{Ml} = $datos[14];
#print "Ml $datos[14] \n";
$mag{Mwp} = $datos[18];
#print "Mwp $datos[18] \n";
$mag{Mb} = $datos[12];
#print "Mb $datos[12] \n";
foreach my $value ( sort { $mag{$a} cmp $mag{$b} } keys %mag ) {
#print "$value $mag{$value}\n";
$m1mag[$cont] = sprintf "%4.2f", $mag{$value};
$m1typ[$cont] = $value;
}
$origen[$cont] = $datos[0];
my @utc = gmtime( $origen[$cont] );
#$origen[$cont] = int($origen[$cont]);
$yr[$cont] = sprintf "%4d", $utc[5] + 1900;
$mo[$cont] = sprintf "%02d", $utc[4] + 1;
$mday[$cont] = sprintf "%02d", $utc[3];
$hr[$cont] = sprintf "%02d", $utc[2];
$mi[$cont] = sprintf "%02d", $utc[1];
$se[$cont] = sprintf "%02d", $utc[0];
my $A = $mday[$cont] . "/" . $mo[$cont] . "/" . $yr[$cont];
my $B = $hr[$cont] . ":" . $mi[$cont] . ":" . $se[$cont];
$tmp_label
= $frame->Label( -text => $i, -padx => 2, -anchor => 'center', -background => 'white', -relief => "groove" );
$T->put( $i, 1, $tmp_label );
$tmp_label
= $frame->Label( -text => $A, -padx => 2, -anchor => 'center', -background => 'white', -relief => "groove" );
$T->put( $i, 2, $tmp_label );
$tmp_label
= $frame->Label( -text => $B, -padx => 2, -anchor => 'center', -background => 'white', -relief => "groove" );
$T->put( $i, 3, $tmp_label );
$tmp_label = $frame->Label(
-text => $lat[$cont],
-padx => 2,
-anchor => 'center',
-background => 'white',
-relief => "groove"
);
$T->put( $i, 4, $tmp_label );
$tmp_label = $frame->Label(
-text => $lon[$cont],
-padx => 2,
-anchor => 'center',
-background => 'white',
-relief => "groove"
);
$T->put( $i, 5, $tmp_label );
$tmp_label = $frame->Label(
-text => $datos[6],
-padx => 2,
-anchor => 'center',
-background => 'white',
-relief => "groove"
);
$T->put( $i, 6, $tmp_label );
$tmp_label = $frame->Label(
-text => $m1mag[$cont],
-padx => 2,
-anchor => 'center',
-background => 'white',
-relief => "groove"
);
$T->put( $i, 7, $tmp_label );
$tmp_label = $frame->Label( -text => "FILE", -padx => 2, -anchor => 'center', -background => 'white',
-relief => "groove" );
$T->put( $i, 8, $tmp_label );
$tmp_label->bind('<Button-1>',\&sfile );
}
$T->pack();
my $button_frame = $mw1->Frame( -borderwidth => 4 )->pack();
$button_frame->Button(-text => "Salir", -padx => 30, -command => sub {exit})->pack();
close FICHERO;
(en archivo adjunto está la pantalla) Sí, es esa la pregunta: yo quiero hacer clic en la última columna (FILE) y crear un txt, con los datos de esa fila y otros datos más. ¿Es posible? ¿O tengo que buscar otra manera? ¡Muchas gracias por la respuesta! y disculpen mi poco conocimiento...
| Adjuntos: |

pantalla.png [ 33.58 KiB | Visto 62 veces ]
|
| Última edición por explorer el 2012-01-31 20:35 @899, editado 3 veces en total |
| Formateado de código con Perltidy y poner marcas Perl |
|
2012-01-31 21:20 @931 |
|
|
 |
explorer
Administrador
|
Registrado: 2005-07-24 18:12 @800 Ubicación: Valladolid, España Mensajes: 10216
|
|
|
Re: bind en Tk
|
Veo que la variable $cont no se actualiza en ningún sitio... Si tuvieras el 'use warnings;' activado, Perl te hubiera avisado del error. Sería algo así... (no probado) Primero, debes intercambiar las líneas 130 y 131, y hacer un cambio en una de ellas. Quedaría así: Using perl Syntax Highlighting $tmp_label->bind('<Button-1>', [ \&sfile, $T ] );
$T->put( $i, 8, $tmp_label);
Es decir, asignamos la captura del evento a la etiqueta, y luego "metemos" la etiqueta en la tabla. Ahora queda por definir la subrutina sfile(): Using perl Syntax Highlighting sub sfile {
my ($widget, $table) = @_; # Recibimos el widget que generó el evento, y la tabla donde ocurrió
my ($fila, $columna) = $table->Posn( $widget ); # Posición de la celda
# si el usuario no puede cambiar los datos de la tabla (porque son Label), podemos guardar al archivo
# los datos que tenemos almacenados en los array
open my $ARCHIVO, '>', 'salida.txt';
print $ARCHIVO join "\t", $fila, $A[$fila], $B[$fila], $lat[$fila], $lon[$fila], $datos[$fila], $m1mag[$fila];
close $ARCHIVO;
}
Pero esto solo funciona si tuvieras la información de la tabla guardada en array (en varios, o más sencillo, uno bidimensional).
_________________ JF^D Perl programming
|
2012-02-01 09:41 @445 |
|
|
princi
Perlero Nuevo
|
Registrado: 2012-01-31 08:00 @375 Mensajes: 7
|
|
|
Re: bind en Tk
|
|
¡¡Muchas Gracias!!
|
|
Página 1 de 1
|
[ 5 mensajes ] |
|
| Reglas del Foro |
No puedes abrir nuevos temas en este Foro No puedes responder a temas en este Foro No puedes editar tus mensajes en este Foro No puedes borrar tus mensajes en este Foro No puedes enviar adjuntos en este Foro
|
|
Socializa |
 |
|