Previous
Common Widget Options 
Next
Now What? 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Some Tk Commands

Some Tk Commands

bind
The bind command associates Perl code with events. If you want to do something when the user double-clicks a item in a listbox or when he/she press any button(say F1), bind is what you need. Lets bind something in the next example.

Syntax:
$widget -> bind(<sequence>, Callback);
<sequence> stands for the sequence of button/mouse presses. It should be given in the following pattern...
<modifier-modifier-type-detail> . For example...
<Control-Alt-Key-t> - Control+Alt+T.
Control and Alt are the modifiers and Key is the type with t as the detail. See the next example and you will understand.


#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;

my $lab = $mw -> Label(-text=> " The Bind command ",
	-font=>"ansi 12 bold") -> pack;
my $lst = $mw -> Listbox() -> pack;
$lst -> insert('end',"Don't double-click this.");
$lst -> insert('end',"Don't double-click this either.");
$lst -> insert('end',"Don't even think of double-clicking this.");
$lst -> insert('end',"You may double-click this.");
$lst -> insert('end',"No. Negative. Nay. Nope. Get the message?");
#Bind the double click event to the list box
$lst -> bind ('<Double-ButtonPress-1>', sub { double() });

my $all_keys = $mw -> Label(-justify=>"left",
	-text=>"Press any of the following...
Control+A
Control+Shift+A
Control+Alt+T
Right click
Control+Escape") -> pack;

#Exit when the escape key is pressed
$mw -> bind('<Key-Escape>', sub { exit });
#Shows a helping dialog box when F1 is pressed
$mw -> bind('<Key-F1>',sub { help(); });
#Binds misc keys.
$mw -> bind('<Control-Key-a>', sub { 
	$mw -> messageBox(-message=>"You pressed Control+A, did'nt you?")
});#Control+A
$mw -> bind('<Control-Key-A>', sub { 
	$mw -> messageBox(-message => "Control+Shift+A, right?"); 
	});#Control+Shift+A
$mw -> bind('<Control-Alt-Key-t>', sub { 
	$mw -> messageBox(-message => "Control, Alt and T"); 
});#Control+Alt+T
$mw -> bind('<ButtonPress-3>', sub { 
	$mw -> messageBox(-message => "The right way to click."); 
});#Right click
$mw -> bind('<Control-Key-Escape>', sub { 
	$mw -> messageBox(-message => "You must be a married man. 
What you pressed remindes married men of what they never will have
 - Control or Escape.");
});#Control+Escape 

MainLoop;

#The helping function
sub help {
	$mw -> messageBox(-message=>"Did you ask of help?
You ain't getting any.
Ha Ha Ha!");
}

#This happens at double-click
sub double {
	$mw -> messageBox(-message=>"You double clicked something.
This script is simple - so it won't display what you clicked on.
But if you want a sript that is able to do that, 
write to me at binnyva, gmail
and I will send you a better script.");
}
Previous
Common Widget Options 
Next
Now What? 

Comments

Azeem at 16 Jul, 2007 11:51
Hi,

Im trying to bind <Control-o> with a Tk::Text widget, according to the documentation text widget has <Control-o> internally bound to insert newline characters. How do i override this and prevent the text widget from inserting newlines when the user presses ctrl + o.
Reply to this.
Anonymous at 07 Jul, 2008 09:23
Hi,
I have created a template with Perl/Tk. I want to pass on the input parameters from the template to the source file. It is actually passing on some values e.g.
Tk::Hashes(00099cc). In the book I have seen that it is pushing the value but showing it's physical memory location. How can I get out of this problem.
Please reply.

With Regards,
Partha Mitra
Reply to this.
Anonymous at 31 Jan, 2009 06:28
how can i call an other perl script to run and get its output an differnet dialog box on clicking the submit button while writing code in perl/tk??
plz help me...........
Reply to this.
Anonymous at 11 Oct, 2009 09:41
I am using Tk::BrowseEntry. A dropdown menu is used which is bound to a routine that populates the list of another dropdown list. but the contents of the second list get appended. i want to refresh.
How??
Reply to this.
Anonymous at 18 Oct, 2009 09:54
"the contents of the second list get appended. i want to refresh."

You need to delete() the old contents, before insert()ing the new contents.
Reply to this.
Anonymous at 08 Jan, 2010 08:15
Thank you very much for your guide. Just one minor correction (to grammar) in the line:
$mw -> bind('<Control-Key-a>', sub {
$mw -> messageBox(-message=>"You pressed Control+A, did'nt you?")
});#Control+A

Instead of "did'nt" it should be "didn't."
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed