Previous
Common Widget Options 
Next
Now What? 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Some Tk Commands

Some Tk Commands

bind
The bind command associates Tcl scripts 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:
bind path <sequence> script
<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.


#The helping function
proc help {} {
	tk_messageBox -message {Did you ask of help?
You ain't getting any.
Ha Ha Ha!}
}

#This happens at double-click
proc double {} {
	tk_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(at)hotmail(dot)com
and I will send you a better script.}
}

label .lab -text " The Bind command " -font {ansi 12 bold}
listbox .lst
.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
bind .lst <Double-ButtonPress-1> { double }

label .keys  -justify left -text {Press any of the following...
Control+A
Control+Shift+A
Control+Alt+T
Right click
Control+Escape}

pack .lab .lst .keys -expand 1 -fill x ;#Pack everything

#Exit when the escape key is pressed
bind . <Key-Escape> { exit }
#Shows a helping dialog box when F1 is pressed
bind . <Key-F1> { help }
#Binds misc keys.
bind . <Control-Key-a> \
 { tk_messageBox -message "You pressed Control+A, did'nt you?" } ;#Control+A
bind . <Control-Key-A> \
 { tk_messageBox -message "Control+Shift+A, right?" } ;#Control+Shift+A
bind . <Control-Alt-Key-t> \
 { tk_messageBox -message "Control, Alt and T" } ;#Control+Alt+T
bind . <ButtonPress-3> \
 { tk_messageBox -message "The right way to click." } ;#Right click
bind . <Control-Key-Escape> \
 { tk_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 
Previous
Common Widget Options 
Next
Now What? 

Comments

pradeep at 12 Jan, 2007 05:10
Thanks for giving such a wonderfulwork in the web.The Tutorial is excellent.
Regards
pradeep
Reply to this.
Fruttenboel at 21 Jul, 2007 05:21
Binny,

I need to access an I/O port from within a TCL/TK script if possible. But I cannot find out how to do it.

Does Tcl/Tk have commands like inport and outport? Or could I use external commands, compiled with my Mocka compiler?
Reply to this.
Binny V A at 22 Jul, 2007 08:47
Unfortunately, this is not an area that I am experienced in. Try the code in this page...
wiki.tcl.tk/447

Or try something like this(not my code)

# Open COM port for reading and writing and assign it to variable called "serial"
set serial [open com4: r+]

#Configure Serial Port
fconfigure $serial -mode "9600,n,8,1" -blocking 0 -buffering none -translation binary

# Set up set tup fileevent to detect incoming data
fileevent $serial readable [list recieveData $serial]

#Send the '$dataToSend' to the serial port
puts $serial $dataToSend

#Called when fileevent detects incoming data
proc recieveData { chan } {
if { [eof $chan] } {
catch {close $chan}
return
}
set data [read $chan]
puts $data
}
Please note that I have not tested the above code - it is strictly a copy/paste example
Reply to this.
JZ at 30 Sep, 2007 02:28
Awesome example. You're the greatest Tk programmer ever. These examples are awesome and I've decided to pick up Tk once again to develop UI in Tk at work. Many many thanks and kudos to you.
Reply to this.
A D Sharanya at 04 Oct, 2007 10:42
excellent tutorial for starters like me.. I am stuck , need ur help.. I want a new GUI ( which can support all functionalities like the listbox , buttons , etc.) to pop up when i hit a button on the main GUI... how do we do this?
Reply to this.
Comment


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