我有一個使用TK gui軟件包的紅寶石程序,我在TkButton,特別是命令部分遇到問題。我試圖在點擊按鈕時運行同一個類中的方法。我的代碼如下。我對Ruby非常陌生,但通常不是編程。當在gui中點擊按鈕時,我得到一個應用程序錯誤,指出「Error:NameError:unknown option ...」,它將按鈕中的方法調用標識爲錯誤原因。有人能解釋我做錯了什麼嗎?我正在使用RubyMine進行開發。Ruby TkButton不會運行proc
代碼:
require 'tk'
require 'test/unit'
require_relative 'calc'
require_relative 'calcTest'
class CalcUIK
def test_add
calc = Calc.new
expected = Calc.add tk6.get().to_i,tk6.get().to_i
tk8['textvariable'] = 'Result: ' + expected
end
hello = TkRoot.new do
title "Hello World"
# the min size of window
minsize(400,400)
end
tk1 = TkLabel.new(hello) do
text 'Super Calculator'
foreground 'red'
pack { padx 15; pady 15; side 'left'}
end
tk5 = TkLabel.new(hello) do
text 'Enter two numbers to math'
foreground 'blue'
pack { padx 15; pady 15; side 'left'}
end
tk6 = TkEntry.new(hello) do
foreground 'blue'
pack { padx 15; pady 15; side 'left'}
end
tk7 = TkEntry.new(hello) do
foreground 'blue'
pack { padx 15; pady 15; side 'left'}
end
tk8 = TkLabel.new(hello) do
textvariable
foreground 'blue'
pack { padx 15; pady 15; side 'left'}
end
tk2 = TkButton.new(hello){
text 'Add'
command (proc {self.test_add})
pack('padx'=>'20')
pack('side'=>'left')
}
end
Tk.mainloop
PS我知道這個代碼是一種愚蠢的,但它僅僅是一個虛擬程序來設置一些更重要的事情了。我需要解決的問題是按鈕單擊不執行test_add方法的原因。謝謝。