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

Hi Perlmonks,

I am learning canvas with Perl Tkx and having problem adding text to the item

created. In my program, a rectangular box is created and a text "testing" is inserted

for the rectangle item. I expect to see the text "testing" next to the rectangular

box but it does not. Please point out what wrong with my codes. Thanks so much.

----------------------------------- use Tkx; $mw = Tkx::widget->new("."); $canvas = $mw->new_tk__canvas; $canvas->g_grid(-column=>0, -row=>0, -sticky=>"nwes"); $mw->g_grid_columnconfigure(0, -weight=>1); $mw->g_grid_rowconfigure(0, -weight=>1); $id = $canvas->create_rectangle(30, 30, 200, 80, -fill => "red", -tags => "first_box"); $canvas->insert($id, 0, "testing"); #<== testing not displayed $canvas->g_bind("<1>", [sub {$canvas->itemconfigure("current",-fill => "green");}]); Tkx::MainLoop();

Replies are listed 'Best First'.
Re: Canvas Text Item
by Loops (Curate) on Jul 24, 2013 at 22:57 UTC
    The code is trying to insert text into a rectangle, which is not an editable type required by that method. You want instead to use the create_text() method:
    $canvas->create_text(80, 40, -text => "testing");

      Thanks thanks thanks for the answer. It works great now.