Re: reusing "code entities" without oop
- From: "Aric Bills" <aric.bills@xxxxxxxxx>
- Date: 29 Mar 2007 08:41:41 -0700
There are ways to do this in pure Tcl. However, if your primary purpose
is to create re-useable Tk components, then I'd recommend using Snit
(http://wiki.tcl.tk/snit). It is itself pure Tcl code, and is very well
polished for this kind of work.
-- Neil
I second this suggestion. Snit is all of 376K (compare to the size of
Python + Tkinter - Tcl/Tk itself), and was designed with Tk GUI
components in mind. It also feels very "tcl-ish" (rather than C+
+ish), which I consider to be a strength.
Here's an example of a (useless) component coded in Snit. It's an
entry widget with a history feature. Hopefully it gives you a feel
for what it's like to write Tk code with Snit.
package require snit
snit::widget mementry {
# an entry widget that remembers previous input
variable history {}
component entry
component button
component historylist
# pass on all unknown options and commands to the entry widget
delegate option * to entry
delegate method * to entry
constructor {args} {
# create widgets
install entry using entry $win.entry
install button using button $win.button \
-text "History" \
-command [mymethod showhistory]
install historylist using menu $win.historylist \
-tearoff 0
# widget geometry management
grid $entry $button -sticky nsew
grid rowconfigure $win 0 -weight 1
grid columnconfigure $win 0 -weight 1
# add to history when user presses Enter
bind $entry <Key-Return> [mymethod addtohistory]
# process any options invoked at widget creation
$self configurelist $args
}
method addtohistory {} {
# called when the user presses <Enter> / <Return>
lappend history [$entry get]
$entry selection range 0 end
}
method showhistory {} {
# called when user clicks on the "History" button
if {[llength $history] == 0} {
return
}
# history mechanism is a popup menu--
# clear any previous entries in the menu,
# then repopulate it with history info
$historylist delete 0 end
foreach item $history {
$historylist insert 0 command \
-label $item \
-command [mymethod selecthistory $item]
}
$historylist post [winfo pointerx $win] [winfo pointery $win]
}
method selecthistory {value} {
# called when the user selects something from the menu
$entry delete 0 end
$entry insert end $value
$entry selection range 0 end
}
method clearhistory {} {
# for programmer's convenience
set history {}
}
}
.
- Follow-Ups:
- Re: reusing "code entities" without oop
- From: Gerald W. Lester
- Re: reusing "code entities" without oop
- References:
- reusing "code entities" without oop
- From: Stephen M. Gava
- Re: reusing "code entities" without oop
- From: Neil Madden
- reusing "code entities" without oop
- Prev by Date: Problem with if/else
- Next by Date: Re: Hosting providers
- Previous by thread: Re: reusing "code entities" without oop
- Next by thread: Re: reusing "code entities" without oop
- Index(es):