Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

-tk questions

by tamaguchi (Pilgrim)
on Jul 26, 2006 at 08:37 UTC ( [id://563730]=perlquestion: print w/replies, xml ) Need Help??

tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I have some questions about -tk.

Somehow I need to specify a longer list of values for this I would like to use a entry box similar to a normal entry box but with many lines. Is this possible to do in -tk?

When the program is finished I save the settings used in this run, for the next run, in a special settingsfile. Is this the way to save the settings or could the program overwrite it self, and in this way save settingsparameters in the program file? If this is possible is it then a good idea?

I would need to browse for files to process by the program. How is this done in the most easy way according to you?

Thank you for any help

Replies are listed 'Best First'.
Re: -tk questions
by GrandFather (Saint) on Jul 26, 2006 at 09:22 UTC

    Use the Text widget for multi-line text editing.

    use warnings; use strict; use Tk; my $mw = MainWindow->new (-title => "editor"); my $text = $mw->Scrolled ('Text', -font => 'normal', -wrap => 'word', -scrollbars => 'e',); $text->pack (); $mw->Button (-text => "open file", -command => \&openFile)->pack (); MainLoop (); sub openFile { my @filetypes = ( ['Text', '.txt', 'TEXT'], ['Text', '.txt', 'TEXT'], ); my $currentFile = $text->getOpenFile (-defaultextension => '.txt', -filetypes => \@filetypes); }

    Self modifying code is generally frowned on as being somewhat difficult to maintain. A setting file would generally be the best way to store persistent data, but there are lots of ways of generating and maintaining the settings file. One fairly easy way to do it is ti use Storable; and keep all your context stuff in a hash. Then you can:

    use Storable; # Load persistent stuff my $persistRef = retrieve ('config') if -e 'config'; # Use stuff my $text = $persistRef->{text} || ''; # ... $persistRef->{text} = $text; # Save stuff store $persistRef, 'config';

    Update: added file open dialog code to Tk sample


    DWIM is Perl's answer to Gödel
Re: -tk questions
by Crian (Curate) on Jul 26, 2006 at 09:05 UTC

    You could use a text field or a few entry fields in a frame.

    You could overwrite your program, perhaps the __DATA__ area at the end, but i think this is a bad idea, better store the information somewhere else.

Re: -tk questions
by zentara (Archbishop) on Jul 26, 2006 at 12:51 UTC
    If you really want the "entry" widget, you could use the Tk::JComboBox. Loading and saving the array from/to __DATA__ is up to you. There is Inline::Files which would allow you to load(and write) different DATA segments.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::JComboBox; my @choices; my $index = 'IM'; foreach my $f ( 'A' .. 'Z' ) { foreach my $s ( 'A' .. 'Z' ) { push @choices, "$f$s"; } } print "@choices"; my $mw = MainWindow->new(); my $jcb = $mw->JComboBox( -relief => 'groove', # -popuprelief => 'groove', -mode => 'editable', -validate => 'match', -highlightthickness => 0, -choices => \@choices, -textvariable => \$index, -selectcommand => sub { print "Selected: $index\n"; } )->pack; MainLoop; #########################

    Simple Inline Files

    #!/usr/bin/perl use Inline::Files; print while <PEANUT>; print while <BUTTER>; __PEANUT__ Bing Bong Bang __BUTTER__ Foo Bar Baz

    And to change a DATA segment

    #!/usr/bin/perl use Inline::Files; use warnings; use strict; #open NUMBER, '+<'; #InlineFiles auto opens my $number = <NUMBER>; seek NUMBER, 0, 0; print "I am number $number\n"; print NUMBER ++$number; __NUMBER__ 0

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found