Reputation: 611
i am very new to ITCL can some one help me how to convert following code from Tcl to itcl
catch { namespace delete ::HVToolSet }
namespace eval ::HVToolSet { } {
}
proc ::HVToolSet::Main {} {
if {[winfo exists .main]} {
destroy .main
}
set ::HVToolSet::base [toplevel .main]
variable tab_frame
set x 200
set y 200
wm geometry $::HVToolSet::base ${x}x${y}+100+0
wm title $::HVToolSet::base "Chevron's Build Effective Stress Results Tool"
wm focusmodel $::HVToolSet::base passive
set creatFrame [frame .main.mnFrame]
pack $creatFrame -side top -anchor nw -expand 1 -fill both -padx 7 -pady 7
button $creatFrame.okbutton -text "OK" -command ::HVToolSet::okcall
pack $creatFrame.okbutton -side top
}
proc ::HVToolSet::okcall {} {
::HVToolSet::checkRun "right"
}
proc ::HVToolSet::checkRun {val} {
set abc 10
::newspace::exec $abc # another name space method calling
}
::HVToolSet::Main
Upvotes: 0
Views: 143
Reputation: 137767
First off, the mapping is not exact. You're moving from a system without classes to one with, and that's a fundamental and subtle difference.
However, crudely, a procedure becomes a method and a namespace becomes a class. That's at least a first approximation for what to do:
package require Itcl
itcl::class HVToolSet {
# Shared over all instances (and unused otherwise?!)
common variable tab_frame ""
# Specific to each instance of this class
private variable base ""
# 'Main' seemed to be a constructor of some sort
constructor {{win .main}} {
if {[winfo exists $win]} {
destroy $win
}
set base [toplevel $win]
set x 200
set y 200
wm geometry $base ${x}x${y}+100+0
wm title $base "Chevron's Build Effective Stress Results Tool"
wm focusmodel $base passive
set creatFrame [frame $base.mnFrame]
pack $creatFrame -side top -anchor nw -expand 1 -fill both -padx 7 -pady 7
button $creatFrame.okbutton -text "OK" -command [itcl::code okcall]
pack $creatFrame.okbutton -side top
}
# Obvious destructor...
destructor {
destroy $base
}
# Callback, best done as private method
private method okcall {} {
$this checkRun "right"
}
# Public method...
method checkRun {val} {
set abc 10
::newspace::exec $abc ; # another name space method calling
}
}
# Make an instance of the class that operates with the window .main
HVToolSet myInstance .main
Working out what needs to be a constructor, what a private method and what a public method can take a bit of thought. Generally, constructors make and initialize things, private methods only make sense within the class (e.g., to handle callbacks or refactor out complex stuff) and public methods may mean something when called externally.
Upvotes: 1