Re: how to open a separate notepad
by thundergnat (Deacon) on Jul 22, 2005 at 08:53 UTC
|
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;
| [reply] [d/l] |
|
|
thanks for your reply thundergnat
this code works only in win2000 how can i make it to work in win98 please suggest a way
| [reply] |
|
|
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";
}
| [reply] [d/l] |
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.
| [reply] [d/l] |
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 | [reply] |
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
| [reply] [d/l] |
|
|
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");
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
| [reply] |
|
|
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.
| [reply] [d/l] |
Re: how to open a separate notepad
by davidrw (Prior) on Jul 22, 2005 at 12:48 UTC
|
First comment is that Grandfather's solution is much better than using notepad if these are supposed to be read-only files. With notepad the user can very easily accidently (or purposefully) delete/edit stuff and then save.
Second comment (assuming you still want to kick off a notepad) is that the above contains good info, especially on the Tk code. But to summarize what the tk code should run (e.g. system vs exec vs fork) I wanted to toss this list into the mix. It's from a recent post of mine from the Start windows .exe thread:
From the "programs and processes" section of Categorized Questions and Answers :
| [reply] |
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
| [reply] [d/l] |