#!/usr/bin/perl -w use strict; use warnings; use Tk; # Globals my @filler = qw( just a bunch of filler words ... ); # Main program my $mw = new MainWindow(-title => 'Detect_if_text_visible'); my @opts = (-bg => 'peachpuff', -width => 32, -height => 16); push @opts, -wrap => "none"; my $txt = $mw->Scrolled('Text', @opts, -scrollbars => "osoe"); $txt->tagConfigure('white', -background => 'white'); $txt->tagConfigure('target', -background => 'red'); $txt->pack(-expand => 1, -fill => 'both'); insert_filler(20); $txt->insert("end", 'TARGET is to the right ==============> ', 'white'); $txt->insert("end", "X", 'target'); $txt->insert("end", ' <=============== TARGET is to the left', 'white'); $txt->insert("end", "\n"); insert_filler(20); $mw->repeat(1000 => \&idle_loop); $mw->bind("" => sub { $mw->destroy() }); MainLoop; # Subroutines sub insert_filler { my ($nlines) = @_; for (my $i = 0; $i < $nlines; $i++) { my $line = (join(" ", @filler) . " ") x 4; my $word = shift @filler; push @filler, $word; $txt->insert("end", "$line\n"); } } sub idle_loop { my $index = $txt->index("target.first"); my $bbox = $txt->bbox($index); if ($bbox) { print "Index '$index' is VISIBLE\n"; } else { print "Index '$index' NOT visible\n"; } }