Key bindings with TkPerl 
Author Message
 Key bindings with TkPerl

Dear Perl programmers,
I'm currently dealing with a little problem concerning to bind the left
mouse button to a top level Canvas widget.
In tcl/tk I wrote this little program:

        canvas .c
        bind .c "<ButtonPress-1>" { puts "." }
        pack .c

When I press the left mouse button I get a "." on my terminal.
OK, so far.

Now I tried this in tkperl:
        use Tk;
        $top = tkinit;
        $c = Canvas::new($top);
        $c-> tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );
        tkpack $c;
        tkmainloop;

Looks quite similar but doesn't work. What did I do wrong?

-------------------------------------------------------------------------------


Rechenzentrum,                                Voice: +49 261 9119-641
Rheinau 1, D-56075 Koblenz, Germany           Fax:   +49 261 9119-499
-------------------------------------------------------------------------------



Tue, 29 Apr 1997 00:07:05 GMT  
 Key bindings with TkPerl

Christoph> I'm currently dealing with a little problem concerning to
Christoph> bind the left mouse button to a top level Canvas widget.

Christoph> Now I tried this in tkperl:
Christoph> use Tk;
Christoph> $top = tkinit;
Christoph> $c = Canvas::new($top);
Christoph> $c-> tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );

This line is wrong.  You really want:

        tkbind($c,"<ButtonPress-1>",sub { print STDERR "." });    

Christoph> tkpack $c;
Christoph> tkmainloop;

The style you were using is for binding an event to a sub-item in a
canvas.  For example:

        use Tk;

        $top = tkinit;
        $c = Canvas::new($top);
        $id = $c->create("rectangle", 10, 10, 80, 80);
        $c->itemconfigure($id, "-width" => 7);
        $mover = new CanvasMover ($c, $id);
        $c->tkbind($id, "<ButtonPress-1>",   "pickup",  $mover);
        $c->tkbind($id, "<B1-Motion>",       "drag",    $mover);
        $c->tkbind($id, "<ButtonRelease-1>", "putdown", $mover);
        tkpack $c;

You'll see how I'm binding <ButtonPress-1> to the object $id in the
canvas $c.  This lets me do a more low level binding.

John


 Workstation Specialist - Worcester Polytechnic Institute College Computer Ctr.
                        Kill your television



Tue, 29 Apr 1997 01:18:11 GMT  
 Key bindings with TkPerl

Quote:
>Dear Perl programmers,
>I'm currently dealing with a little problem concerning to bind the left
>mouse button to a top level Canvas widget.
>In tcl/tk I wrote this little program:
>    canvas .c
>    bind .c "<ButtonPress-1>" { puts "." }
>    pack .c
>When I press the left mouse button I get a "." on my terminal.
>OK, so far.
>Now I tried this in tkperl:
>    use Tk;
>    $top = tkinit;
>    $c = Canvas::new($top);
>    $c-> tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );
>    tkpack $c;
>    tkmainloop;
>Looks quite similar but doesn't work. What did I do wrong?

Hello Christoph,

below "my" programm, that works. just throw away that $c->

#!/usr/local/bin/perl -- # -*- perl -*-
use Tk;
$top = tkinit;
$c = Canvas::new($top);
tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );
tkpack $c;
tkmainloop;

I noticed the fault, when I tried with Entry::new; then perl complained!

Quote:
>-------------------------------------------------------------------------------


>Rechenzentrum,                                Voice: +49 261 9119-641
>Rheinau 1, D-56075 Koblenz, Germany           Fax:   +49 261 9119-499
>-------------------------------------------------------------------------------

--
                            _________________________________________________
  _  __   _|                   Andreas Lohrum,     Josef-F"uhrer-Str. 43
 (_| | | (_| |_|               +49 89 8131709,     D-80997 M"unchen



Tue, 29 Apr 1997 09:30:15 GMT  
 Key bindings with TkPerl


Quote:

>> In tcl/tk I wrote this little program:
>>        canvas .c
>>        bind .c "<ButtonPress-1>" { puts "." }
>>        pack .c
>> Now I tried this in tkperl:
>>        use Tk;
>>        $top = tkinit;
>>        $c = Canvas::new($top);
>>        $c-> tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );

        ^^^^^
  I think this should be:
             tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );

# because "tkbind" is not a method of "Canvas".

Quote:
>>        tkpack $c;
>>        tkmainloop;



Fri, 02 May 1997 22:15:00 GMT  
 Key bindings with TkPerl

Quote:

>Dear Perl programmers,
>I'm currently dealing with a little problem concerning to bind the left
>mouse button to a top level Canvas widget.
>In tcl/tk I wrote this little program:

>    canvas .c
>    bind .c "<ButtonPress-1>" { puts "." }
>    pack .c

>When I press the left mouse button I get a "." on my terminal.
>OK, so far.

>Now I tried this in tkperl:
>    use Tk;
>    $top = tkinit;
>    $c = Canvas::new($top);
>    $c-> tkbind ($c, "<ButtonPress-1>", sub { print STDERR "." } );

        ^^^^
        That shouldn't be there

Quote:
>    tkpack $c;
>    tkmainloop;

>Looks quite similar but doesn't work. What did I do wrong?

tkbind is a command in its usual incarnation, just as bind is in Tcl/Tk.
When you use it as a method of Canvas, it means to bind an action to
an item in the canvas (in which case the first argument is the item id.

Another difference between your perl example and your Tcl example is that
the perl print statement doesn't tack on a newline whereas Tcl puts does.
Since STDERR is not buffered (by default), you'll still see the dots though.

--Malcolm
--

Oxford University Computing Services
"Widget. It's got a widget. A lovely widget. A widget it has got." --Jack Dee



Sat, 03 May 1997 01:22:32 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. overriding tkperl default key bindings?

2. Problem with binding events on TkPerl canvas

3. Tkperl: How do you get bindings from inheritance?

4. tkperl default binding question !!

5. Setting new bindings in TkPerl

6. problem with key bindings in Tk-b3

7. newbie help with anonymous subroutine syntax for text widget key binding

8. Create a key binding on a canvas?

9. Key Bindings for Listbox

10. Key Bindings for Listbox

11. Binding Return Key

12. Key repeat problem in bindings.

 

 
Powered by phpBB® Forum Software