http://www.perlmonks.org?node_id=361299

One of the most asked questions in Tk is "how do I print out my canvas, or at least get a screenshot?" Well the answers I've always seen is export it using the "postscript function". Well, that is trickier to use than one would hope for.

Here is a simple demo of grabbing screenshots of the root window, the mainwindow, and just the canvas. It is linux only and uses Tk::WinPhoto, a basic part of Tk.

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::WinPhoto; use Tk::JPEG; my $mw = tkinit; my $canv = $mw->Canvas(width => 300, height => 200)->pack(); # Create line inside the canvas $canv->create ('line',1, 1, 100, 100, -fill=>'red'); $canv->createRectangle(10,20,30,40, -fill=>'blue' ); my $fullbutton = $mw->Button(-text=>'Full Screen Capture', -command => \&full_capture, )->pack; my $mainbutton = $mw->Button(-text=>'MainWindow Capture', -command => \&mw_capture, )->pack; my $canvbutton = $mw->Button(-text=>'Canvas Capture', -command => \&canv_capture, )->pack; MainLoop; sub full_capture{ my @id = grep{$_ =~ 'Window id'} split("\n",`xwininfo -root`); my @ids = split(' ',$id[0]); (my $id) = grep{$_ =~ /0x/} @ids; my $image = $mw->Photo(-format => 'Window', # # -data => oct($mw->id) # # -data => oct('0xa00022') -data => oct($id) ); my $pathname = './rootwindow.'.time.'.jpg'; $image->write($pathname, -format => 'JPEG'); } ######################################################### sub mw_capture{ my $image = $mw->Photo(-format => 'Window', -data => oct($mw->id) # # -data => oct('0xa00022') # -data => oct($id) ); my $pathname = './mainwindow.'.time.'.jpg'; $image->write($pathname, -format => 'JPEG'); } ########################################################## sub canv_capture{ my $image = $mw->Photo(-format => 'Window', -data => oct($canv->id) ); my $pathname = './canvas.'.time.'.jpg'; $image->write($pathname, -format => 'JPEG'); } ##############################################################