Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greetings BrowserUk!

I just recently came across the infamous Canvas bind problem again.

According to Mastering Perl/Tk (published by O'Reilly), you need to use a different bind method with a Canvas object, called CanvasBind.  (This is described in chapter 9.4, and it's because a Canvas object has its own bind method).

Here's an example program I created to demonstrate.  When you resize the main window (and thus the Canvas object), it draws random squares as a result of the change in size and/or position (the event for which is a "<Configure>"):

#!/usr/bin/perl -w + # Strict use strict; use warnings; + # Libraries use Tk; + # Subroutines sub random($) { int rand $_[0] } sub random_square($) { my $can = shift; my ($x0, $y0, $x1, $y1) = (random 256, random 256, random 256, ran +dom 256); $x1 += $x0; $y1 += $y0; my $bg = sprintf "#%02x%02x%02x", random 256, random 256, random 2 +56; $can->createRectangle($x0, $y0, $x1, $y1, -fill => $bg); } # Main program my $mw = new MainWindow(-title => "Canvas binding test"); my $can = $mw->Canvas(-bg => 'white', -width => 512, -height => 512); $can->pack(-expand => 1, -fill => 'both'); $can->CanvasBind('<Configure>', [\&random_square]); MainLoop;

@ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

In reply to Re^3: Tk how-to? by liverpole
in thread Tk how-to? by BrowserUk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-24 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found