How can I show Perl console output in a GUI?
I have several simple 开发者_运维知识库Perl programs writing to the standard output, but this has some problems:
- some of my users are scared by a console
- my users work on Windows, so my program output is displayed on a
cmd
console, so I cannot control the way it is displayed (colors and terminal size), and worse, the console is not resizeable (at least in width)
To solve these problems, I would like to be able to display the output in a simple GUI interface as an option. The GUI should be responsive until the program finishes (it should be possible to scroll or resize the window while it's running).
What simple toolkit and widget can I use to do this? (I develop on Windows with ActivePerl).
You can use any GUI option you like, and then you could use Tie::STDOUT to redefine the behavior of print
and printf
to the STDOUT filehandle to instead dump output into the widget of your choice. The only thing is that getting it to talk to your widgets across packages cleanly using the anonymous sub might be messy. Here's a short, crude example using Win32::GUI:
use Win32::GUI();
use Tie::STDOUT
print => sub {
$main::textfield->Append(@_);
};
my $main = Win32::GUI::Window->new(
-name => 'Main',
-text => 'Perl',
-width => 220,
-height => 230,
);
our $textfield = $main->AddTextfield(
-name => "Output",
-left => 8,
-top => 8,
-width => 180,
-height => 180,
-readonly => 1,
-multiline => 1,
-vscroll => 1,
);
$main->Show();
sub Main_Terminate {
-1;
}
if(!fork()) {
print "Hello.\n";
for (1..20) {
sleep 1;
printf "More output %d\n", $_;
}
} else {
Win32::GUI::Dialog();
}
Note the call to Win32::GUI::Dialog()
at the end is present to keep the window from closing as soon as the script is finished.
From a quick search you do have a few options:
- Perl with NCurses (its a GUI, but it keeps it in the console)
- Perl with wxWindows wxPerl
- PerlTK
- GTK with Perl (see the wikibooks reference)
Also, there is a wikibooks reference for this.
If you have Firefox installed on the machines, I've been working on the module XUL::Gui
which lets you display your Perl gui using Firefox's rendering engine. Building on Adam's answer:
use XUL::Gui;
use Tie::STDOUT
print => sub {$ID{text}->value .= join '' => @_};
display Window title=>'Perl', minwidth=>640, minheight=>480,
TextBox( FILL SCROLL id=>'text', multiline=>'true' ),
delay {
print "hello world\n"; # Output goes to the window.
for (1..5) {
printf "More output %d\n", $_;
}
};
Edit: fixed a bug with multi-line return values from the gui, example above is nicer now. works with XUL::Gui 0.35+
wxWidgets
using Wx
CPAN module is popular in Perl world right now (see Padre
)
However I'm not sure this comes with ActivePerl which I believe may come with Tk
instead.
/I3az/
Win32::Console::ANSI lets you control background and text colors and locations, and the size and title of the command line window.
AFAIK, it doesn't allow mouse input, but it may be enough if you're just displaying program progress.
Another possibility is to rewrite the program in html/javascript and have full interactive capability.
I often do my heavy-lifting in perl, then construct and write out an html program for user interaction.
Or have a standard html program and let the perl write out a json file with the specific data before I invoke the html program.
精彩评论