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'); } ##############################################################

In reply to Tk Screen and Canvas Screenshots by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.