首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Tcl到Itcl的转换

从Tcl到Itcl的转换
EN

Stack Overflow用户
提问于 2013-06-05 14:59:02
回答 1查看 119关注 0票数 0

我是ITCL的新手,有人能帮我把下面的代码从Tcl转换成itcl吗

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2013-06-05 21:00:50

首先,映射不准确。你正在从一个没有类的系统转移到一个有类的系统,这是一个基本的和微妙的区别。

但是,简单地说,过程变成了方法,命名空间变成了类。这至少是要做什么的第一近似值:

代码语言:javascript
复制
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

弄清楚什么是构造函数,什么是私有方法,什么是公共方法需要花点心思。一般而言,构造函数创建和初始化东西,私有方法只在类内有意义(例如,处理回调或重构出复杂的东西),而公共方法在外部调用时可能有意义。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16933461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档