Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

chooseDirectory set Default

by lil_v (Sexton)
on Jul 11, 2008 at 13:45 UTC ( [id://696960]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to make the user select the directory he wants to save files in using the chooseDirectory method. The problem I have is that I want the user to choose the directory once and it should be set for the next time he opens the program again. I can't seem to set the user's input as default for the program. Here is my code:
#!/usr/bin/perl use Tk; use Tk::JPEG; use Tk::Dialog; my $mw = MainWindow->new; my $c = $mw->Canvas( -tile => $tile, qw/-width 1500 -height 750/); $c->pack(-side => 'left'); $path = $c->Label(-text => "Default Path For Saving Input Files", -font => 'Garamond 10 bold') ->place(-relx => 0.23, -rely => + 0.4); $path_text = $c->Entry(-textvariable => \$def_path, -width => 50)->place(-relx => 0.4, -rely => 0.4); $path_button = $c->Button(-text => "Browse", -font => 'Garamond 8 bold', -width => 5, -command => \&browse)->place(-relx => 0.65, -rely => 0.4); MainLoop; sub browse { $def_path = $path_button->chooseDirectory(-title => 'Select direc +tory', -initialdir => '.', -mustexist => 1); }
Any Suggestions?

Replies are listed 'Best First'.
Re: chooseDirectory set Default
by olus (Curate) on Jul 11, 2008 at 13:56 UTC

    If you want the user to have a directory set by default the next time he runs your program, then you'll have to 'save state'. Try using a config file, where you write the directory the user chose, and have your program reading that file at the begining and look for that info. The first time the program is executed that information will not be there, but there will be something the next time.

    update: below is some code you can adapt to include in your program. Error checking on dealing with the file is not in the script, as I don't know TK (and how to, visually, alert the user in case of errors) and I would have the script dieing. Run the script and look at config.txt's contents, and then modify the value of the variable $new_default, run it again and look for differences.

    use strict; use warnings; my $file = "config.txt"; my $default_dir = ""; my @config; my $content; if (-f $file) { open CONF, "<$file"; $content = <CONF>; close CONF; if ($content =~ /DEFAULT_DIR/) { @config = split /:/,$content; $default_dir = $config[1]; } } my $new_default = "default_path"; if($new_default ne $default_dir) { open CONF, ">$file"; print CONF "DEFAULT_DIR:".$new_default; close CONF; }

    After all this, do go and take a look at open. It is important that you know how to deal with files.

      I've never used a config file before. Do you have any examples on how it could be done?

        For something as simple as the program your making (at least you made it appear simple), you can simply write the data in a fashion such as:

        initdir:/home/user initbuttonstate:active

        et cetera.

        Of course, then open the config and gather the information. Depending on the complexity of the config file, you can just split each line on a colon, and use the second variable of the array.

        <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      thx

        Sure mate. But this time you gotta promise you will read open's docs :).

        First point: when you read the file, and since you are adding more lines, you have to get all those lines. Also, you are appending a new line to the end of each conf variable, so there is the need to chomp that new line.

        if (-f $file) { open CONF, "<$file"; my @contents = <CONF>; close CONF; foreach $content (@contents) { my @config = split /-/,$content; if ($content =~ /DEFAULT_IN/) { $default_in = $config[1]; chomp($default_in); } elsif ($content =~ /DEFAULT_OUT/) { $default_out = $config[1]; chomp($default_out); } } }

        Second point: Writing the info back to the file. I don't know what those browse methods are, but if you need to write in two separate steps, then you'll have to write all the info both times.

        print CONF "DEFAULT_IN-$def_path\nDEFAULT_OUT-$def_path_out\n";

        As a side note, you should look for a way of being able to write only if any of the paths actually changed.

        if( ($def_path ne $default_in) || ($def_path_out ne $default_out) ) { # at least one of the values changed, write both }

        update lil_v updated his node, so now this doesn't make much sense.

Re: chooseDirectory set Default
by zentara (Archbishop) on Jul 11, 2008 at 16:47 UTC
    You should always use warnings; and use strict. Otherwise you will experience bugs. Anyways, you can store your default dir in the DATA section of your script, you can also use Inline::Files.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; use Tk::Dialog; my $def_path; #defaults to current dir in DATA # read last stored value while(<DATA>){ if($_ =~ /^DIR:(.*)$/){ $def_path = $1 } } my $mw = MainWindow->new; # make sure def_dir gets written when program closes $SIG{INT} = sub { &close_it_up }; $mw->protocol('WM_DELETE_WINDOW' => sub { &close_it_up }); my $c = $mw->Canvas( # -tile => $tile, qw/-width 1500 -height 750/)->pack(-side => 'left'); my $path = $c->Label(-text => "Default Path For Saving Input Files", #-font => 'Arial 18 bold' )->place(-relx => 0.23, -rely => 0.4); my $path_text = $c->Entry(-textvariable => \$def_path, -width => 50)->place(-relx => 0.4, -rely => 0.4); my $path_button = $c->Button(-text => "Browse", #-font => 'Arial 8 bold', -width => 5, -command => [\&browse, $def_path])->place(-relx => 0.65, -r +ely => 0.4); MainLoop; sub browse { my $init_dir = shift; $def_path = $mw->chooseDirectory(-title => 'Select directory', -initialdir => $init_di +r, #-mustexist => 1 # scre +ws up on linux ); } sub close_it_up{ open(SELF,"+<$0")||die $!; while(<SELF>){last if /^__DATA__/} truncate(SELF,tell SELF); $def_path ||='.'; print SELF 'DIR:'; print SELF "$def_path"; print SELF $/; truncate(SELF,tell SELF); close SELF; Tk::exit; exit; } ############################################################ __END__ __DATA__ DIR:'.'

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: chooseDirectory set Default
by psini (Deacon) on Jul 11, 2008 at 13:48 UTC

    Only a wild guess: maybe the option "-initialdir" in the call to chooseDirectory is what you are looking for?

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      Well not really, because when the user restarts the program the set Directory is back to blank again. I dont want to set the initial directory for the user, I want the user to set the default directory just once and he must be able to see the set directory next time he starts the program instead of setting it each time he runs the program. I don't know if I'm being clear, let me know if you want me to explain more clearly... thx

        Yes, it's clear, but if you want your program to remember the user choice after program termination, you need to store this info outside the program.

        Natural choices could be a database (if your application uses one) or a configuration file.

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Log In?
Username:
Password:

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

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

    No recent polls found