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

hi everybody iam a biologist if my question looks stupid please pardon me iam developing a plant database using perl Tk.I have a lot of articles in notepad(textfiles). now after doing a search i will identify certain textfiles and retrieve the link and display it along with a button now my problem is once i click the button i need to display the coresponding textfile a separate window. please suggest me a way of how to open a notepad by the click of a buttom in perl Tk.

Replies are listed 'Best First'.
Re: how to open a separate notepad
by thundergnat (Deacon) on Jul 22, 2005 at 08:53 UTC

    Very simplistic example. Modify to suit.

    use strict; use warnings; use Tk; my $top = MainWindow->new; $top->geometry('100x100'); my $file = 'C:/Autoexec.bat'; $top->Button( -text => 'Click Me', -command => sub{ system "start C:/Windows/Notepad.exe $file" } )->pack; MainLoop;
      thanks for your reply thundergnat this code works only in win2000 how can i make it to work in win98 please suggest a way

        I have a routine I've used in the past when I needed start up an external executable. This will work cross-platform on Win95, Win98, WinME, Win2k, WinXP, Linux, OSX and probably many others. The only caveat is that you must have no file named "spawn.pl" in the directory your script resides in. You will probably need to adjust the calling parameters for different OS's (It is unlikely that Linux or OSX will have an executable called "NotePad"), but in general this is pretty cross platform.

        It is essentially just a fork and exec, without explicitly doing one. Useful, because it is extremely difficult to fork a program running under Perl/Tk, (under Windows, at least), This neatly sidesteps the problem.

        use strict; use warnings; use Tk; my $top = MainWindow->new; $top->geometry('100x100'); $top->Button( -text => 'Open A File', -command => sub{ my $types = [ ['Text Files', [qw/.txt .text .bat/]], ['All Files', ['*']], ]; my $filename = $top->getOpenFile(-title => 'Open File' +); if (defined $filename) { spawn('Notepad.exe', $filename) } } )->pack; MainLoop; sub spawn{ # Routine to spawn another perl process and use it to ex +ecute an external program my $args = join ' ', @_; unless (-e 'spawn.pl'){ open my $spawn, '>', 'spawn.pl'; print $spawn 'exec @ARGV;'; } if ($^O =~ /Win/) { $args = '"'.$args.'"'; }else{ $args .= ' &'; } system "perl spawn.pl $args"; }
Re: how to open a separate notepad
by BrowserUk (Patriarch) on Jul 22, 2005 at 08:49 UTC

    Assumes you have files junk{1,2,3}.dat in the current directory.

    use Tk; my $mw = new MainWindow(); my $content; my $text = $mw->Text()->pack(); $text->Insert( "junk1.dat\njunk2.dat\njunk3.dat\n" ); my $ok = $mw->Button( -text => "View in Notepad", -command => sub{ my $file = $text->getSelected; system 1, "notepad $file"; } )->pack(); MainLoop;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: how to open a separate notepad
by marto (Cardinal) on Jul 22, 2005 at 08:41 UTC
    Hi,

    Are you asking how to open an instance of notepad.exe from within your TK program?
    If so you may want to look at Win32::Process. The cpan documentation gives opening notepad.exe as an example.

    On the other hand, you could open another TK window which displays the data from your text file.

    Hope this helps

    Martin
Re: how to open a separate notepad
by davorg (Chancellor) on Jul 22, 2005 at 08:54 UTC

    The other people replying to your question probably know far more about Windows and Perl/Tk programming than I do, but it seems to me that your simplest option would be to run something like:

    system("notepad $path_to_text_file");
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      system will wait for notepad to finish executing before the Perl code continues. If you wanted to just launch notepad and continue then you would use exec.

      Similarly if you just wanted to start the default application then you can omit the program and just exec the file. In this situation, exec may be essential because the default editor may support multiple documents and the user wouldn't necessarily want to close it. The Perl application won't have to wait.

      #launch notepad explicitly exec ("notepad $path_to_text_file"); # or launch the default application exec ("$path_to_text_file");

        exec is not such a good choice as it does not return. Click the first button, and that's all she wrote mate.

        Best answer probably is to simply open another top level Tk window to display the text for the additional file.

        I'm a newbie at Tk so I can't provide good code to do that, but it's likely to be fairly easy. Maybe I'll try after I've done the shopping :)


        Perl is Huffman encoded by design.

        I think you mean "fork" and then "exec" in the new child process.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: how to open a separate notepad
by GrandFather (Saint) on Jul 22, 2005 at 11:50 UTC

    The following code allows a number of top level windows to be opened by clicking a button in the main window. A little work will get OP to creating a Text widget in each window to display the text files.

    use strict; use warnings; use Tk; my $main = MainWindow->new; $main->Button (-text => "new window", -command => \&newTextWnd)->pack +(); MainLoop; sub newTextWnd { $main->Toplevel (); }

    Perl is Huffman encoded by design.
Re: how to open a separate notepad
by davidrw (Prior) on Jul 22, 2005 at 12:48 UTC
Re: how to open a separate notepad
by radiantmatrix (Parson) on Jul 22, 2005 at 17:46 UTC

    I suggest that you don't want to launch Notepad per se, but whatever text editor the user has associated with the .txt extension. Windows, starting at least with Win2k, will open an associated application when the data file is executed. Therefore, the following opens Notepad with filename.txt loaded on most systems:

    sub { system 1,"filename.txt" }

    Of course, you'll want to replace "filename.txt" with a variable or function call or something to determine which filename you'll be opening.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law