我正在运行Windows + Ruby2.3并且正在弄乱tk
库。我试图让它绘制一个不同颜色的矩形网格,但每当我尝试向画布添加一个形状时,我的脚本就会崩溃。这是代码的精简版本:
require 'tk'
require 'tkextlib/tile'
root = TkRoot.new
content = Tk::Tile::Frame.new(root)
canvas = TkCanvas.new(content)
line = TkcLine.new( canvas, 0, 0, 10, 10, :fill => 'red' )
Tk.mainloop
然而,当我运行它时,我得到以下错误+回溯:
C:/Ruby23/lib/ruby/2.3.0/tk/itemconfig.rb:115:in `hash_kv': wrong argument type nil (expected Array) (TypeError)
from C:/Ruby23/lib/ruby/2.3.0/tk/itemconfig.rb:115:in `itemconfig_hash_kv'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:722:in `_parse_create_args'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:735:in `create'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:758:in `create_self'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:751:in `initialize'
from C:/nopathforyou.rb:9:in `new'
from C:/nopathforyou.rb:9:in `<main>'
任何人都知道怎么办?提前谢谢。
答案 0 :(得分:4)
我遇到了同样的错误,我最后通过添加以下代码解决了这个问题:
module TkItemConfigOptkeys
def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])
hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
end
end
应该在'require'语句之后,比如说,你的代码应该是:
require 'tk'
require 'tkextlib/tile'
module TkItemConfigOptkeys
def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])
hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
end
end
root = TkRoot.new
content = Tk::Tile::Frame.new(root)
canvas = TkCanvas.new(content)
line = TkcLine.new( canvas, 0, 0, 10, 10, :fill => 'red' )
Tk.mainloop
抱歉我的英文。
添加之后,我的代码运行正常。