1

尝试在 Ruby/Tk 中添加一个窗格窗口,我收到以下错误:

C:/Users/user/Ruby193/lib/ruby/1.9.1/tk.rb:3016:in `_invoke': Attempt to change read-only option (RuntimeError)

每当我将 orient 选项添加到我的代码中时,如下所示:

p = Tk::Tile::Paned.new(parent) { orient 'horizontal' }

出于某种原因,“orient”似乎是只读的(默认为“vertical”)?我注意到网上有一个 ruby​​/tk 教程,其中有一个 Paned 窗口示例,它避免使用 orient 选项,可能是因为它们遇到了同样的错误?

如果您将以下教程代码粘贴到 .rb 文件中并运行它(无 orient 选项),它可以工作。添加类似于上面的 orient 选项,它会失败。

require 'tk'
require 'tkextlib/tile'

$resultsVar = TkVariable.new
root = TkRoot.new
root.title = "Window"

p = Tk::Tile::Paned.new(root)do
  height 110
  place('height' => 100, 'width' => 200, 'x' => 10, 'y' => 10)
  #orient 'horizontal' # <== uncomment this line to see error
end

f1 = TkFrame.new(p) {
  relief 'groove'
  borderwidth 3
  background "red"
  padx 30
  pady 30
  pack('side' => 'left', 'pady' => 100)
}
f2 = TkFrame.new (p){
  relief 'groove'
  borderwidth 3
  background "yellow"
  padx 30
  pady 30
  pack('side' => 'right', 'pady' => 100)
}

p.add f1 #, nil <== had to remove nil option here because this also caused an error
p.add f2 #, nil

Tk.mainloop

有没有其他人能够让“东方”选项起作用?我需要它是水平的,而不是默认的垂直值。我尝试查看 tk.rb 并跟踪错误跟踪,它似乎表明存在“method_missing”问题。

4

1 回答 1

1

我认为问题在于属性“orient”可以设置但不能更改。如果您在创建时传递选项,则可以创建 PanedWindow 'horizo​​ntal'。喜欢

p = Tk::Tile::Paned.new(root, 'orient' => 'horizontal' )

忘记'method_missing'调用。是动态创建小部件属性的技巧。

于 2013-04-21T00:08:56.903 回答