新手问题 关于 ruby/tk 里面 proc () 的问题

africwildman · January 01, 2015 · Last by africwildman replied at January 01, 2015 · 1711 hits
require 'tk'
require 'win32ole'

$w=WIN32OLE.new("excel.application")
$workbooks=$w.workbooks.add "1.xls"

TkButton.new(nil){
    text "打印"
    command proc{print $workbooks.class}
}.pack(:fill=>'x')
TkButton.new(nil){
    text "退出"
    command proc{
        $w.quit
        exit}
}.pack(:fill=>'x')

Tk.mainloop

上面代码里为什么第一个 button 调用的 proc{print $workbooks.class} 可以运行 第二个 button 调用的 command proc{ $w.quit exit} } 就出来 NoMethodError: unknown property or method: `quit' ?

可以$w.methods 列出看有没有 quit 吧。

有的啊,调用 proc{}外部的对象,有时候就能调用成功,有时候就不能,无法理解啊。 “打印”那个 button 的我是想 proc{$workbooks.worksheets[1].__PrintOut},但也是 NoMethodError

#1 楼 @chenge 有的啊,调用 proc{}外部的对象,有时候就能调用成功,有时候就不能,无法理解啊。 “打印”那个 button 的我是想 proc{$workbooks.worksheets[1].__PrintOut},但也是 NoMethodError

You need to Sign in before reply, if you don't have an account, please Sign up first.