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

use strict; use Tk; use Tk::DialogBox; #Character Variables. my $name = "Na"; my $health = "HP"; my $morality = "Mo"; my $willpower = "WP"; my $potency = "Po"; my $resource = "Re"; my $target = 0; # MainWindow my $mwin = new MainWindow(-background=>'black'); #Button for adding character information my $chrb = $mwin->Button(-text=>'Add Character',-activeforeground=>'bl +ack', -activebackground=>'green',-foreground=>'green',-backgroun +d=>'black', -command=>\&charadd) -> pack(-anchor=>'w'); #Canvas, which character info is drawn on. my $canv = $mwin->Scrolled('Canvas',width=>600,height=>600,-background +=>'black',-confine=>0) -> pack(); $canv->createLine(0,20,600,20,-fill=>'green'); #DialogBox, for when a new character is added. my $diag = $mwin->DialogBox(-title=>'Add Character',-buttons=>['Ok','C +ancel'], -default_button=>'Ok'); $diag->add('Entry',-textvariable=> \$name) -> pack(); $diag->add('Entry',-textvariable=> \$morality) -> pack(); $diag->add('Entry',-textvariable=> \$willpower) -> pack(); $diag->add('Entry',-textvariable=> \$potency) -> pack(); $diag->add('Entry',-textvariable=> \$resource) -> pack(); $diag->add('Entry',-textvariable=> \$health) -> pack(); Tk::MainLoop; #Maintain GUI. $canv->bind('<current>','<Button-1>', =>\&damage); our $charc = 0; sub charadd() { ++$charc; $canv->createText(20,($charc*60)+10,-text=>$charc,-fill=>'green'); $diag->Show(); $canv->createText(40,($charc*60)+10,-text=>$name,-fill=>'green'); $canv->createText(160,($charc*60)+10,-text=>"Po: $potency",-fill=> +'green'); $canv->createText(220,($charc*60)+10,-text=>"WP: $willpower",-fill +=>'green'); $canv->createText(280,($charc*60)+10,-text=>"Re: $resource",-fill= +>'green'); $canv->createText(360,($charc*60)+10,-text=>"Mo: $morality",-fill= +>'green'); for (my $i = 1; $i <= $health; $i++) { $canv->createRectangle($i*30,($charc*60)+20,($i*30)+20,($charc +*60)+40, -fill=>'green',-tag=>'hpbox'); } } sub damage() { }
It runs, but when I click on one of the healthboxes drawn it doesn't do anything.
Once the script ends it feeds me this huge error that ultimately ends in:
Usage $widget->bind(...) ... Derived.pm line 469

Replies are listed 'Best First'.
Re: Tk Canvas Item Bind
by Anonymous Monk on Dec 17, 2014 at 03:07 UTC

    MainLoop is loop, its responsible for drawing the GUI

    when MainLoop ends, there is no more GUI, no more windows, the program is over

    Don't try to bind or other stuff after MainLoop

    Some tips/links to tips on writing GUIs in Re^3: TK Gui Help