Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, As I can't find this in the Camel or bookshelf, it is probably so obvious that no one thought to explain it. However, can anyone tell me how to put embed a Button widget in a Canvas? Thanks heaps, Brian.

Replies are listed 'Best First'.
Re: PTk,. Button in a canvas?
by ariels (Curate) on Apr 23, 2002 at 06:26 UTC

    Tk's widget demo has precisely what you want, in "The canvas item types" demo. It also lets you see the code.

    Basically, you want to use Tk::Canvas' createWindow command to display a child widget in the canvas:

    use Tk; my $mw = new Tk::MainWindow; my $c = $mw->Canvas(-width => 300, -height => 100)->pack; my $text = $c->createText(20, 20, -text => 'A text item'); my $but = $c->Button(-text => 'Push me!', -command => sub { colour_text('red'); $mw->after(500, [\&colour_text => 'black']); }, ); my $but_i = $c->createWindow(70, 50, -window => $but); MainLoop; sub colour_text { my $colour = shift; $c->itemconfigure($text, -fill => $colour); }