首页 > 代码库 > Mac OS X下让ruby支持tcl/tk
Mac OS X下让ruby支持tcl/tk
我记得在老早在OS X10.8下使用ruby1.9.x的时候只要到下载安装ActiveTcl8.5,没怎么配置就运行tk好好的。但是近日想重新执行下tk代码,发现在require ‘tk‘的时候就报错鸟!现在用的是ruby2.1.3版本。上网搜了一下,1.9.x之后的版本默认不再将tcl/tk支持编译到ruby里面,so如果你想用则必须重新编译ruby!我勒个去!就不能做成个gem吗?但在多种外围尝试无果的的情况下只有一条路,就是重新编译ruby了...
到ruby官网下载最新的ruby2.1.5的源代码15MB左右,解压后257MB,console进入解压目录,输入:
./configure -prefix=/Users/apple/src/ruby_src/ruby2.1.5_installed --with-arch=x86_64,i386 --enable-pthread --enable-shared --with-tk --with-tcl
注意-prefix必须要绝对路径,否则不让过;这是你要最后安装ruby2.1.5的路径,一旦固定下来,就不可以再动了。因为我后来用rvm也安装了ruby2.1.5版,所以这里换了个临时位置,只是测试用的。接着可以输入make命令编译ruby,大概要等一会,最后执行make install就将ruby2.1.5安装到了我前面设定的位置上。下面测试一下对tk的支持如何:
apple@kissAir: bin$ls
erb gem irb rake rdoc ri ruby testrb
apple@kissAir: bin$./ruby -e "require ‘tk‘"
可以看到是ok的,然后运行一下我的tk.rb看一下效果(代码节选):
require 'dbm' #require 'tempfile' #require 'zip/zip' #require 'win32api' #PATH = '/Users/apple/src/c_src/ruby-1.9.3-p362/ext/tk/lib/' require 'tk' #require PATH+'tk' require 'tkextlib/tkimg/bmp' require 'tkextlib/tkimg/jpeg' require 'tkextlib/tkimg/png' require 'tkextlib/iwidgets' require 'tkextlib/bwidget' SM_CXSCREEN = 0 SM_CYSCREEN = 1 def get_screen_size #get_screen_size_nv = Win32API.new('user32','GetSystemMetrics',['L'],'L') #x = get_screen_size_nv.Call(SM_CXSCREEN) #y = get_screen_size_nv.Call(SM_CYSCREEN) `./s`.split.map {|x| x.to_i} #[x,y] end def zip(path) zip_path = path + '.zip' File.delete(zip_path) if File.exist? zip_path Zip::ZipFile.open(zip_path,Zip::ZipFile::CREATE) {|f| f.add(File.split(path)[1],path)} end def unzip(path) unzip_path = path[0..-5] Zip::ZipFile.open(path) {|f| f.extract(f.entries[0],unzip_path) {true}} end DB_PATH = File.expand_path(".") + '/movies.dbm' #zip(DB_PATH + '.dir') #zip(DB_PATH + '.pag') #unzip(DB_PATH + '.dir.zip') #unzip(DB_PATH + '.pag.zip') db = DBM.new(DB_PATH) #db.clear at_exit { db.close #压缩数据库 #zip(DB_PATH + '.dir') #zip(DB_PATH + '.pag') #File.delete(DB_PATH + '.dir');File.delete(DB_PATH + '.pag') } ROOT_W = 1000 ROOT_H = 610 x,y = get_screen_size #计算屏幕居中显示时需要移动到的坐标 mov_x = (x - ROOT_W)/2 mov_y = (y - ROOT_H)/2 root = TkRoot.new(:title=>"movie query v1.2") Tk::Wm.geometry(root,"+#{mov_x}+#{mov_y}") #Tk::Wm.attributes(root,:toolwindow=>true) Tk::Wm.geometry(root,"#{ROOT_W}x#{ROOT_H}") Tk::Wm.minsize(root,ROOT_W,ROOT_H) Tk::Wm.maxsize(root,ROOT_W,ROOT_H) img_love = TkPhotoImage.new(:file=>File.expand_path(".") + '/logo.gif') Tk::Wm.iconphoto(root,img_love) def dlgbox_input_name(in_name) #puts Tk::Wm.state dlg #Tk::Wm.deiconify dlg dlg = Tk::Toplevel.new(:title=>"输入演员名") #Tk::Wm.attributes(dlg,:toolwindow=>true) Tk::Wm.attributes(dlg,:notify=>true) #隐藏窗口 #Tk::Wm.withdraw dlg out_name = nil txt_input_name = Tk::Tile::Entry.new(dlg,:width=>40) do grid :row=>0,:column=>0 end txt_input_name.value = http://www.mamicode.com/in_name>界面丑是丑了点,不过后期可以再调整哦,关键是这段代码windows,linux,OS X下跑都没问题啊,轻松跨平台鸟:
还有一种办法是用rvm重新安装支持tcl/tk的版本:
rvm reinstall 2.1.5 --enable-shared --enable-pthread --with-tk --with-tcl
不过我试了一下,在brew update无限卡住了,所以才有了开篇的那一坨文章...
最后本猫觉得这样用不太爽,尤其是自己编译的irb用不了,因为其还是调用了rvm的路径。要替换也很容易,做软连接吧:
apple@kissAir: rubies$ls -lh
total 16
lrwxr-xr-x 1 apple staff 35B 12 10 13:03 default -> /Users/apple/.rvm/rubies/ruby-2.1.5
drwxr-xr-x 8 apple staff 272B 7 27 19:47 ruby-2.1.2
lrwxr-xr-x 1 apple staff 45B 12 10 16:08 ruby-2.1.5 -> /Users/apple/src/ruby_Src/ruby2.1.5_installed
drwxr-xr-x 8 apple staff 272B 12 10 13:02 tmp_ruby-2.1.5
先把原来的ruby目录改名,然后将编译的路径加入即可,可以看到其版本号显示略有不同:
apple@kissAir: rubies$ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [universal.x86_64-darwin14.0]
apple@kissAir: rubies$./tmp_ruby-2.1.5/bin/ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
Mac OS X下让ruby支持tcl/tk