in reply to Adding text to a Perl/Tk canvas

I want ice cream with pistachios, chocolate crumbles, and cherries on top. :-)

Seriously, what have you tried? It's not going to be easy to satisfy all those requirements, but it can be done with some work. Number 3 will be difficult, but not impossible. Here is a start for you, how to drag text to a position. All you need to do is open a Popup, with a text widget.... get your text, on pressing the Ok button, put it on the canvas, and drag it to where you want it.

#!/usr/bin/perl use warnings; use strict; use Tk; # parts ripped from the testGraphics Zinc demo # and shown by themselves for clarity my $dx; my $dy; my $mw = MainWindow->new; $mw->geometry("700x600"); $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14)); my $canvas = $mw->Canvas(-width => 700, -height => 565, -bg => 'black', -borderwidth => 3, -relief => 'sunken', )->pack; my $closebutton = $mw->Button(-text => 'Exit', -command => sub{Tk::exi +t(0)}) ->pack; my $dragster = $canvas->createText( 50, 75, -fill => 'red', -tags => ['move'], -text => 'yahoo', -font => 'big', ); $canvas->bind('move', '<1>', sub {&mobileStart();}); $canvas->bind('move', '<B1-Motion>', sub {&mobileMove();}); $canvas->bind('move', '<ButtonRelease>', sub {&mobileStop();}); MainLoop; sub mobileStart { my $ev = $canvas->XEvent; ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); $canvas->raise('current'); print "START MOVE-> $dx $dy\n"; } sub mobileMove { my $ev = $canvas->XEvent; $canvas->move('current', $ev->x + $dx, $ev->y +$dy); ($dx, $dy) = (0 - $ev->x, 0 - $ev->y); print "MOVING-> $dx $dy\n"; } sub mobileStop{&mobileMove;}

I'm not really a human, but I play one on earth CandyGram for Mongo

Replies are listed 'Best First'.
Re^2: Adding text to a Perl/Tk canvas
by merrymonk (Hermit) on Jun 10, 2008 at 21:22 UTC
    Many thanks for that.
    I do not want to sound ungrateful, however, with the previous good answers you have given I think I may have been able to do what you sent.
    It was Number 3 that I really wanted help with since I really
    do not know where to start.
    I suppose part of the problem is that I do not understand how
    your Perl actually get the selected text (and any other canvas items) from the canvas on to the cursor.
    It may be that $canvas->raise('current'); does this but I do not see how. :-(
    Any more clues or references will be more than welcome!
      Start by abandoning your problem for a few hours and reading an introductory Tk tutorial, like this one
Re^2: Adding text to a Perl/Tk canvas
by Anonymous Monk on Jul 12, 2008 at 02:34 UTC
    wow this is a really good code! nice piece! i tried changing the 'move' in this code $canvas->bind('move', '<1>', sub {&mobileStart();}); $canvas->bind('move', '<B1-Motion>', sub {&mobileMove();}); $canvas->bind('move', '<ButtonRelease>', sub {&mobileStop();}); into 'current' so that i can move different layers of objects inside the canvas. WOW thanks man! you guys are great! man im starting to love perl right now hehehe.