Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Tk: how to have an editable/movable label?

by mawe (Hermit)
on Oct 17, 2004 at 08:12 UTC ( [id://399895]=note: print w/replies, xml ) Need Help??


in reply to Tk: how to have an editable/movable label?

Hi!

use Tk; my $top = new MainWindow; my $c = $top->Canvas()->pack(); my ($x,$y); # left mouse-button -> create text $top->bind('<1>' => [\&click_left, Ev('x'),Ev('y')]); # shift and left mouse-button -> edit text $top->bind('<Shift-Button-1>' => [\&edit, Ev('x'),Ev('y')]); # right mouse-button -> move the text $top->bind('<3>' => [\&move_it, Ev('x'),Ev('y')]); MainLoop; sub click_left { my $e = shift; ($x,$y) = @_; my $t = $top->Toplevel(); my $entry = $t->Entry()->pack(); $entry->focus(); $t->bind('<Return>' =>sub{ my $text = $entry->get(); $c->createText($x,$y,-text=>$text); $t->destroy(); }); } sub move_it { my $e = shift; ($x,$y) = @_; my $id = $c->find('closest',$x,$y); $top->bind('<Button3-Motion>' => [\&move, Ev('x'),Ev('y'),$id]); } sub move { my ($e,$xm,$ym,$id) = @_; my $dx = $xm - $x; my $dy = $ym - $y; ($x,$y) = ($x+$dx, $y+$dy); $c->move($id,$dx,$dy); } sub edit { my $e = shift; ($x,$y) = @_; my $id = $c->find('closest',$x,$y); my $text = $c->itemcget($id,'text'); my $t = $top->Toplevel(-title=>"edit"); my $entry = $t->Entry()->pack(); $entry->insert(0,$text); $entry->focus(); $t->bind('<Return>' => sub { my $text = $entry->get(); $c->delete($id); $c->createText($x,$y,-text=>$text); $t->destroy(); }); }
I'm sure this is not the best way, but it should do what you want :-)

mawe

Replies are listed 'Best First'.
Re^2: Tk: how to have an editable/movable label?
by johnnywang (Priest) on Oct 17, 2004 at 19:19 UTC
    That's great, mawe. Thanks a lot, I tried it, works great.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://399895]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found