There are two things I've been able to do with embedding an xterm into a Tk widget. The first is just embedding the xterm, the second is setting up an io-slave so that you can write to that xterm from an external process. The second one I did with Zinc, but you should be able to modify it for a canvas, if you need help let me know.
#!/usr/bin/perl -w
use strict;
use Tk;
my $mw = MainWindow->new();
my $canv = $mw->Canvas(-bg => 'lightsteelblue',
-relief => 'sunken',
-width => 500,
-height => 400)->pack(-expand => 1, -fill => 'both');
my $xtermWidth = 400;
my $xtermHeight = 300;
## this Frame is needed for including the xterm in Tk::Zinc
my $xtermContainer = $canv->Frame(-container => 1);
my $xtid = $xtermContainer->id();
print "$xtid\n";
# converting the id from HEX to decimal as xterm requires a decimal Id
+
my ($xtId) = sprintf hex $xtid;
print "$xtId\n";
my $label = $canv->createText( 250,10,
-text => "Hide xterm",
);
my $dcontitem = $canv->createWindow(250,175,
-window => $xtermContainer,
-width => $xtermWidth+4,
-height => $xtermHeight+4,
-state => 'normal');
$canv->bind($label, '<1>', \&hideShow);
sub hideShow {
if ($canv->itemcget($label, -text) =~ /Hide/) {
$canv->itemconfigure($label,
-fill => 'yellow',
-text => "Show xterm");
$canv->itemconfigure($dcontitem, -state => 'hidden');
} else {
$canv->itemconfigure($label,
-fill => 'black',
-text => "Hide xterm");
$canv->itemconfigure($dcontitem, -state => 'normal');
}
}
my $width = $xtermWidth/10;
my $height = $xtermHeight/20;
$mw->Button(-text => "Toplevel", -command => \&do_Toplevel)->pack( );
system("xterm -fn 10x20 -geometry ${width}x${height} -into $xtId &");
MainLoop();
sub do_Toplevel {
my $tl = $mw->Toplevel(-use=>$xtId );
$tl->title("Toplevel");
$tl->Button(-text => "Close",
-command => sub { $tl->withdraw })->pack;
}
__END__
and here is an IO-slave example
#!/usr/bin/perl -w
use strict;
use Tk;
use Tk::Zinc;
use Proc::ProcessTable;
my $title = time();
my $xtermparentpid;
my $pttydev;
my $id;
my $count = 0;
my $mw = MainWindow->new();
my $zinc = $mw->Zinc(-backcolor => 'gray',
-relief => 'sunken',
-width => 400,
-height => 300)->pack(-expand => 1,
-fill => 'both');
my $xtermWidth = 300;
my $xtermHeight = 200;
## this Frame is needed for including the xterm in Tk::Zinc
my $xtermContainer = $zinc->Frame(-container => 1);
my $xtid = $xtermContainer->id();
# converting the id from HEX to decimal as xterm requires a decimal Id
+
my ($xtId) = sprintf hex $xtid;
my $label = $zinc->add('text', 1,
-text => "Hide xterm",
-position => [150, 30]);
my $dcontitem = $zinc->add('window', 1,
-window => $xtermContainer,
-position => [50, 75],
-width => $xtermWidth+4,
-height => $xtermHeight+4,
-visible => 1);
$zinc->bind($label, '<1>', \&hideShow);
sub hideShow {
if ($zinc->itemcget($label, -text) =~ /Hide/) {
$zinc->itemconfigure($label,
-color => 'yellow',
-text => "Show xterm");
$zinc->itemconfigure($dcontitem, -visible => 0);
} else {
$zinc->itemconfigure($label,
-color => 'black',
-text => "Hide xterm");
$zinc->itemconfigure($dcontitem, -visible => 1);
}
}
my $width = $xtermWidth/10;
my $height = $xtermHeight/20;
system("xterm -T $title -fn 10x20 -geometry ${width}x${height} -into
+$xtId &");
my $button = $mw->Button(-text => "Start", -command => \&do_slave_writ
+e)->pack();
my $exit = $mw->Button(-text => "Exit", -command => sub{Tk::exit})->pa
+ck();
MainLoop();
sub do_slave_write{
$button->configure(-text => 'Stop', -command => \&do_slave_stop);
$mw->update;
my $t = new Proc::ProcessTable;
foreach my $p (@{$t->table}) {
if($p->cmndline =~ /$title/){$xtermparentpid = $p->pid}
}
foreach my $p (@{$t->table}) {
if($p->ppid == $xtermparentpid){$pttydev = $p->ttydev}
}
open(FH,">$pttydev") or warn $!;
$id = Tk::After->new($mw,1000,'repeat',
sub{print STDOUT $count,"\n"; print FH $count.'a',"\n";$count++
+});
}
sub do_slave_stop{
$id->cancel;
$button->configure(-text => 'Start', -command => \&do_slave_write);
$mw->update;
# $count=0; close FH; #to reset count
}
I'm not really a human, but I play one on earth.
flash japh
|