Home » Featured, Unix/Linux

How to create scrollable input form with textbox popup in TCL/TK

28 August 2009 1,030 views No Comment

TCL/TK is the very convient script language to create GUI on X-windows platform. Basically it wraps the call of X-Motif and allows user to create GUI very quickly without worry about X-motif functions..

Thought TCL is quite easy to use, it misses some useful functions. One of that is scrolling. Recently I was stuck-ed in similar problem. My requirement was to create a scrollable page with possibility to enter data by user.

Tcl/Tk only allows to attach scroll bar with Entry, Listbox and canvas widgets. If I use canvas then I can not create textbox(entry) on canvas that is scrollable with the canvas. Entry and label widgets created on canvas are not scrollable.

I took another approach. I created text on the canvas and bind the mouse click of text to a procedure. This procedure opens the toplevel widget on the current window with one entry widget and OK/Cancel buttons. Now on OK click data entered in the text box is passed to parent window and the text in the parent window is updated. This is not straightforward way but that’s I could do with TCL.

I have created a simple example of my original problem, which is described below.

Below program creates a scrollable canvas and put 3 texts on it. Clicking on it will open text box to enter the value. Note that I have not put any validation on it as its just a demo program.. Edit it according to your need. I took TCL TK practical programming book as reference

# PROGRAM START

#!/usr/bin/wishx
frame .f

canvas .f.c -width 300 -height 100 -scrollregion {0 0 500 500} -xscrollcommand {.f.sx set} \
-yscrollcommand {.f.sy set}
scrollbar .f.sx -orient horizontal -command {.f.c xview}
scrollbar .f.sy -orient vertical -command {.f.c yview}
pack .f.sx -side bottom -anchor s -fill x
pack .f.sy -side right -anchor e -fill y
pack .f.c -anchor nw -fill both -expand y
pack .f -fill both -expand y

set ROW_NUMBER 1

proc Popup { FieldValue FieldTag } {

global Value

set Value(result) $FieldValue
set f [toplevel .prompt -borderwidth 10]
message $f.msg -text “Enter New value ”
entry $f.entry -textvariable Value(result)
set b [frame $f.buttons -bd 10]
pack $f.msg $f.entry $f.buttons -side top -fill x

bind $f.entry <Return> {set Value(ok) 1}
bind $f.entry <Control-c> {set Value(ok) 0}
button $b.ok -text OK -command {set Value(ok) 1}
button $b.cancel -text Cancel -command {set Value(ok) 0}

pack $b.ok
pack $b.cancel

focus $f.entry
tkwait variable Value(ok)
destroy $f
if {$Value(ok)} {
.f.c itemconfigure $FieldTag -text $Value(result)
set {$FieldName} $Value(result)
} else {
return {}
}
}

set NameValue “No Value”
.f.c create text 10 [expr $ROW_NUMBER *25] -anchor nw -text “Enter Name :”
.f.c create text 200 [expr $ROW_NUMBER *25] -anchor nw -text $NameValue -tag NameValueTag

.f.c bind NameValueTag <Button-1> “Popup {$NameValue} NameValueTag”

set ROW_NUMBER [expr $ROW_NUMBER + 1]

set LastNameValue “No Value”
.f.c create text 10 [expr $ROW_NUMBER *25] -anchor nw -text “Enter Last Name :”
.f.c create text 200 [expr $ROW_NUMBER *25] -anchor nw -text $LastNameValue -tag LastNameValueTag

.f.c bind LastNameValueTag <Button-1> “Popup {$LastNameValue} LastNameValueTag”

set ROW_NUMBER [expr $ROW_NUMBER + 1]

set TelNoValue “No Value”
.f.c create text 10 [expr $ROW_NUMBER *25] -anchor nw -text “Enter Telephone No :”
.f.c create text 200 [expr $ROW_NUMBER *25] -anchor nw -text $TelNoValue -tag TelNoValueTag

.f.c bind TelNoValueTag <Button-1> “Popup {$TelNoValue} TelNoValueTag”

wm title . “Popup Demo”

# PROGRAM END

(No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.