使用ruby tk读取文件,也不太容易,首先要知道怎么使用选择文件的对话框,然后根据选择的文件,读取此文件的路径,其它的事相对来说,就好办了。
# coding: gbk
require 'tk'
root = TkRoot.new{
title "Window"
minsize(200,200)
}
file=TkVariable.new
button = TkButton.new(root) do
text "选择文件"
pack("side" => "left", "padx"=> "20", "pady"=> "50")
end
fileText = TkText.new(root) do
height 5
width 50
pack("side" => "left", "padx"=> "20", "pady"=> "50")
end
button_click = Proc.new {
file = Tk.getOpenFile
fileText.insert 'end',file+"\n"
}
button.comman = button_click
Tk.mainloop
上面的代码中,只有那个insert,不看文档,完全不知道怎么用,其它的还好吧。
效果如下:
![](http://static.oschina.net/uploads/space/2014/0805/171402_sgX2_147032.png)
只要知道文件路径,其它的代码该怎么写还怎么写,就不再多说了。