Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

using Tkx new_ttk__entry AND tk___getOpenFile

by x-lours (Sexton)
on Dec 09, 2011 at 13:44 UTC ( [id://942658]=perlquestion: print w/replies, xml ) Need Help??

x-lours has asked for the wisdom of the Perl Monks concerning the following question:

Hello i'm a french newby in Perl.

I try to create a script which filter a selected file.

goal :

in a 'tk___getOpenFile' I choose my file.

in two 'new_ttk__entry' i want to write

1 / the number of the column of the filter

2 / the filter itself.

i create a new file with the rows containing the filter(1) in the column(2).

i wrote this code :

#!/usr/bin/perl -w use warnings; use strict; use Data::Dumper; use Tkx; my ($lacolonne, @lefiltre); my $types = [ ['CSV Files', ['.csv']], ['Text Files', ['.txt', '.text']], ['All Files', '*'],]; my $fichier = Tkx::tk___getOpenFile( -title => "Sélection du fichie +r à filtrer", -filetypes => $types); unless (-f $fichier) { print "Fichier '$fichier' non trouvé.\n"; exit 2; } unless (@lefiltre) { my $parametre; saisir_2valeurs(\$lacolonne, \$parametre); @lefiltre = qw/$parametre/; } exit 3 unless ($#lefiltre); open IN, $fichier or die "Problème à l'ouverture de $fichier E/S: $!\n +"; while ( <IN> ) { my @enreg = split( /;/, $_ ); for my $filtrage (@lefiltre) { if ($enreg[$lacolonne] =~ /$filtrage/) { open OUT, ">> filtrer_fichier_$filtrage.txt" or die "filtr +er_fichier_$filtrage.txt E/S: $!\n"; print OUT $_ ; close OUT; } } } close IN; =head1 NAME filtre_fichierCSV.pl =head1 SYNOPSIS Interactive mode: perl filtre_fichierCSV.pl <numéro> <filtre> =head1 DESCRIPTION il s'agit de filtrer un fichier sur la colonne <numéro> avec le filtre + <filtre>. le fichier sur lequel appliquer ce filtre est choisi par une fenêtre d +e sélection (Tkx). le fichier généré s'appelle 'filtrer_fichier.txt' =cut sub saisir_2valeurs { use Tkx; my ($saisie1, $saisie2) = @_; my $mw = Tkx::widget->new("."); $mw->g_wm_title("Saisir le type"); my $frm = $mw->new_ttk__frame(-padding => "2 3 12 12"); $frm->g_grid(-column => 0, -row => 0, -sticky => "nwes"); my $e_colonn = $frm->new_ttk__entry(-width => 9, -textvariable => +$saisie1); $e_colonn->g_grid(-column => 2, -row => 1, -sticky => "we"); my $e_filtre = $frm->new_ttk__entry(-width => 9, -textvariable => +$saisie2); $e_filtre->g_grid(-column => 2, -row => 2, -sticky => "we"); my $cb = $frm->new_ttk__button(-text => "Valider la saisie", -comm +and => sub { $mw->g_destroy; }); $cb->g_grid(-column => 3, -row => 3, -sticky => "w"); $frm->new_ttk__label(-text => "Le numéro de colonne du filtre :")- +>g_grid(-column => 1, -row => 1, -sticky => "e"); $frm->new_ttk__label(-text => "Le filtre à extraire (qw) :")- +>g_grid(-column => 1, -row => 2, -sticky => "e"); foreach (Tkx::SplitList($frm->g_winfo_children)) { Tkx::grid_configure($_, -padx => 5, -pady => 5); } $e_colonn->g_focus; $mw->g_bind("<Return>", sub { $mw->g_destroy; }); $mw->g_tkwait_window; Tkx::MainLoop(); print "dans la fenetre $saisie1,$$saisie1; $saisie2,$$saisie2.\n"; }

but i can't write in any of the 2 'new_ttk__entry' !

Why ?

they seem to be disabled or in readonly mode, i don't know ...

can anyone explain me what is the problem ? and of course how to solve it ;-)

best regards

PS

with one 'tk___chooseDirectory' and one 'new_ttk__entry', i cant write in the 'new_ttk__entry'.

Replies are listed 'Best First'.
Re: using Tkx new_ttk__entry AND tk___getOpenFile
by Anonymous Monk on Dec 10, 2011 at 04:36 UTC

    Hello i'm a french newby in Perl.

    What are the implications of this? I mean le implicationes?

    update: I was right $lacolonne, its in like la french :)

    update: pod lines newline after commands paragraphs, as in =head1 NAME\n\nNAME - the is le

    If you use scriptdist it will insert a template pod of the right form, like

    =head1 NAME $script_name - this script does something =head1 SYNOPSIS =head1 DESCRIPTION =head1 AUTHOR =head1 COPYRIGHT =cut

    update: Oh, so it prompts for a csv file, i give a random one, then prompts something in french, and yes, i see the problem, can't write in the entries.

    This works

    #!/usr/bin/perl -- use strict; use warnings; use Tkx; local $Tkx::TRACE=64; my $the_shining = "Here's Johnny! 0"; my $mw = Tkx::widget->new("."); my $b = $mw->new_ttk__button( -text => 'Hello, world', -command => sub { $the_shining =~ s/(\d+)/$1+1/e; return; }, ); $b->g_pack; my $e = $mw->new_ttk__entry( -width => 9, -textvariable => \$the_shining, ); $e->g_pack( -expand => 1, -fill => 'both', ); Tkx::MainLoop(); __END__

    For CSV, you should use one of Text::CSV// Text::CSV_XS/Text::xSV

    See also tkx grid example and tutorial

    update: the entries in your program can get focus if your program loses focus, then gains focus again. I have no idea what is at work here :/ but you can do this programatically

    $mw->g_wm_withdraw(); $mw->g_wm_deiconify(); Tkx::MainLoop();

    For future reference, esp GUI problems, its best to trim all file operations from your program :) See How do I post a question effectively?

      Thanks a lot !

      you find the trouble : loosing focus. your code is efficient and is enough for me.
      $mw->g_wm_withdraw(); $mw->g_wm_deiconify(); Tkx::MainLoop();

      also thanks for the tutorial.

      i was looking for some ;-)

      i found this code :
      Tkx::focus(-force => $mw);
      which works well also. (when you know what your are searching for ...)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://942658]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found