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

Greetings All,
I am trying to bind the mouse click actions to 2 separate sub-routines, eg: one click will bind to sub_one and double click will bind to sub_two, below is the script (I am new to Tk and was helped by monk .dave. to produce this script), please brethrens enlighten me.
And the Other thing is, the $output_text window appears to me that it will only display text, is it possible to include iconic display as well as text?
use Tk; use strict; use Tk::Text; use Tk::Frame; use Tk::DirTree; use Tk::Adjuster; use Tk::Scrollbar; my $mw = new MainWindow; $mw->geometry('500x600'); my $left_frame = $mw->Frame; my $right_frame = $mw->Frame; my $dir_label = $mw->Label(-text => 'Path :'); my $dir_name = $mw->Entry(-width => 30, -textvariable => \ my $variabl +e); my $adjuster = $mw->Adjuster(-widget=>$left_frame, -side=>'left'); my $list_box = $left_frame->Scrolled('DirTree', -height=>'0', -width=> +'0', -scrollbars =>'e',); my $output_text = $right_frame->Scrolled('Text', -height=>'1', -width= +>'1', -scrollbars=>'osoe',); $top_frame->pack(qw/-side top/); ####Errors occure in this '#' enclosed section####################### $list_box->bind('<ButtonRelease-1>', sub {my $indx = $list_box->cursel +ection(); my $selected_object = $list_box->get($indx);}); $list_box->bind('<ButtonRelease-2>', sub{print “\nuser clicked twice”; +}); ##################################################################### $dir_label->pack (qw/-side top -anchor w/); $dir_name->pack (qw/-side top -anchor e/); $left_frame->pack(qw/-side left -fill y/); $adjuster->pack(qw/-side left -fill y/); $right_frame->pack(qw/-side right -fill both -expand 1/); $list_box->pack(qw/-side left -fill both -expand 1/); $output_text->pack(qw/-side bottom -fill both -expand 1/); MainLoop; exit();
Many thanks in advance….

Replies are listed 'Best First'.
Re: Binding mouse clicks in perl/Tk
by Abigail-II (Bishop) on Jul 19, 2002 at 17:12 UTC
    ButtonRelease-1 binds to the event "Release of the first Button". ButtonRelease-2 binds to the even "Release of the second button".

    What you want is ButtonPress-1 and Double-ButtonPress-1.

    See also the Tk::bind manual page.

    As for your second question, yes, you can put images and other widgets in a Text widget. For details, see the Tk::Text manual page.

    Abigail

      Got it, Thanks alot....
      I must say this, I believe that, the best I ever done was to learn Perl and finding PerlMonks web site....I have gone all misty now.....
Re: Binding mouse clicks in perl/Tk
by ichimunki (Priest) on Jul 19, 2002 at 17:03 UTC
    You have bound probably not the exact thing you wanted. This binds button release for button one (left button) and two (middle button). Try
    $list_box->bind('<Button-1>', sub { my $indx = $list_box->curselection(); my $selected_object = $list_box->get($indx); }); $list_box->bind('<Double-Button-1>', sub{ print “\nuser clicked twice”; });
    All of this good event binder information is in perldoc Tk::bind.