I have several simple Perl programs writing to the standard output, but this has some problems:
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:
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+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With