新手问题 用 ocra 打包有调用到 Tk 的脚本时出错!

736248591 · 2015年02月10日 · 最后由 736248591 回复于 2015年02月20日 · 3512 次阅读

错误代码如下:

c:>ocra test-tk.rb === Loading script to check dependencies D:/Ruby193-2/lib/ruby/1.9.1/tk/console.rb:13:in _create_console': interpreter is deleted (RuntimeError) from D:/Ruby193-2/lib/ruby/1.9.1/tk/console.rb:13:increate' from D:/Ruby193-2/lib/ruby/1.9.1/tk/console.rb:15:in <module:TkConsole> ' from D:/Ruby193-2/lib/ruby/1.9.1/tk/console.rb:6:in' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:474:in const_get' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:474:inblock (3 levels) in attempt_load_autoload' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:468:in each' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:468:inblock (2 levels) in attempt_load_autoload' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:466:in each' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:466:inblock in attempt_load_autoload' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:460:in loop' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:460:inattempt_load_autoload' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:706:in build_exe' from D:/Ruby193-2/lib/ruby/gems/1.9.1/gems/ocra-1.3.3/bin/ocra:1165:inblock in '

test-tk.rb 的代码如下:

require 'tk'
root = TkRoot.new { title "Hello, World!" }
TkLabel.new(root) do
   text 'Hello, World!'
   pack { padx 15 ; pady 15; side 'left' }
end
Tk.mainloop

我家的系统是 win7-64,所用的是 rubyinstaller-1.9.3-p550 以及它自带的 TK 库,但是我试过 Ruby 2.1.5-p273 也是报错。有哪位高手知道怎么解决的吗?

得把 tk 的库路径也打包进去……

最好还要检测 ocra 环境然后执行的部分……

官方文档应该都有写……

#1 楼 @Kabie 请问一下要怎么做?谢谢!

https://github.com/larsch/ocra/issues/29 有讨论一些办法:

ocra --no-autoload --add-all-core tkapp.rb c:\ruby193-p392\lib\tcltk

#3 楼 @lyfi2003 哦!太感谢了!

#3 楼 @lyfi2003 您好,有个问题想请教一下。 我用 require 加载外部的代码块(调用到 Tk 库) (如

$root_filedir = File.dirname(__FILE__)
require "#{$root_filedir}/scripe/manager.rb"

)怎样才能将它们用 ocra 打包在一起?

#5 楼 @736248591 OCRA 编译说明有说明多文件会自动打包依赖:https://github.com/larsch/ocra#compilation

但我想,你应该这样:

设定一个统一的加载目录,例如:

在主文件中用:$LOAD_PATH.unshift( File.dirname(__FILE__) )

主文件中直接用相对目录 require: require script/manager

over. 这样的话 OCRA 应该会自动帮你搞定依赖。

#6 楼 @lyfi2003 好快!天哪!谢谢!但又有一个问题冒出来了——tk 无法加载图片 运行 ruby 代码

ruby C:\AGMK_Teamwork_Link_Manager\root.rb

加载图片的局部代码如下

@@error_image = TkPhotoImage.new("file" => "image/miku_cry.gif")
     image @@error_image

错误代码如下:

RuntimeError: couldn't open "image/miku_cry.gif": no such file or directory
---< backtrace of Ruby side >-----
D:/Ruby193/lib/ruby/1.9.1/tk.rb:2054:in `_invoke'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:2054:in `_ip_invoke_core'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:2090:in `_tk_call_core'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:2118:in `tk_call_without_enc'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:81:in `initialize'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:191:in `initialize'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:58:in `block (2 levels) in new'
<internal:prelude>:10:in `synchronize'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:57:in `block in new'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:56:in `instance_eval'
D:/Ruby193/lib/ruby/1.9.1/tk/image.rb:56:in `new'
C:/AGMK_Teamwork_Link_Manager/scripe/enter_atlm.rb:193:in `block in creat_popups_image'
C:/AGMK_Teamwork_Link_Manager/scripe/enter_atlm.rb:192:in `new'
C:/AGMK_Teamwork_Link_Manager/scripe/enter_atlm.rb:192:in `creat_popups_image'
C:/AGMK_Teamwork_Link_Manager/scripe/enter_atlm.rb:146:in `popups'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1456:in `cb_eval'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1456:in `eval_cmd'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1456:in `cb_eval'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1403:in `call'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1599:in `block in callback'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1598:in `catch'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1598:in `callback'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1305:in `mainloop'
D:/Ruby193/lib/ruby/1.9.1/tk.rb:1305:in `block in <module:TkCore>'
---< backtrace of Tk side >-------
    invoked from within
"rb_out c00002"
    invoked from within
".w00000.w00003.w00009 invoke"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list $w invoke]"
    (procedure "tk::ButtonUp" line 24)
    invoked from within
"tk::ButtonUp .w00000.w00003.w00009"
    (command bound to event)

另外,采用您的方法后还是报错

C:/Users/ADMINI~1/AppData/Local/Temp/ocrC789.tmp/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- scripe/manager.rb (LoadError)
        from C:/Users/ADMINI~1/AppData/Local/Temp/ocrC789.tmp/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
        from C:/Users/ADMINI~1/AppData/Local/Temp/ocrC789.tmp/src/root.rb:8:in `<main>'

我用的命令是

ocra --no-autoload --add-all-core C:\AGMK_Teamwork_Link_Manager\root.rb D:\Ruby193\lib\tcltk C:\AGMK_Teamwork_Link_Manager 

#7 楼 @736248591

Both files, directoriess and glob patterns can be specified on the command line. Files will be added as-is. If a directory is specified, OCRA will include all files found below that directory. Glob patterns (See Dir.glob) can be used to specify a specific set of files, for example:

ocra script.rb assets/**/*.png

自己多看看它的文档,试试吧,问题解决最多给你一个思路。

#8 楼 @lyfi2003 哦。可以了。谢谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号