【发布时间】:2015-03-29 17:34:51
【问题描述】:
我想将一些 (~30) CheckButtons 放入位于 BWidget::ScrolledWindow 内的 BWidget::ScrollableFrame 中。出于测试目的,我想将 ScrollableFrame 的大小限制为 100 像素 x 100 像素。我期望ScrollableFrame,大小为100x100,能够滚动浏览所有网格CheckButtons,但窗口会自动扩展,以便所有CheckButtons 都适合TkRoot。
这是我尝试过的:
#!/usr/bin/env ruby
require 'tk'
require 'tkextlib/bwidget'
class TestClass < TkRoot
attr_reader :checkbutton
def initialize(*args)
minsize(800, 400)
@checkbutton = []
for i in 0..29
@checkbutton.push({:name => "checkbutton #{i}"})
end
createGUI()
end
def createGUI
TkLabel.new(self, :text => 'first label').grid({:column => 0, :row => 0, :sticky => 'w'})
scrolledwindow = Tk::BWidget::ScrolledWindow.new(self).grid({:column => 0, :row => 1, :sticky => 'w'})
scrolledwindow.auto('none') # want to see if scrollbars are attached correctly
scrollframe = Tk::BWidget::ScrollableFrame.new(scrolledwindow).grid({:column => 0, :row => 0, :sticky => 'w'})
scrollframe.height(100)
scrollframe.width(100)
# leads to error: /usr/lib/ruby/1.9.1/tk.rb:215:in `class_eval': window name "frame" already exists in parent (RuntimeError)
#sftest = scrollframe.get_frame
scrolledwindow.set_widget(scrollframe)
@checkbutton.each_with_index { |cb, index|
TkCheckButton.new(scrollframe, :text => cb[:name]).grid({:column => 0, :row => index, :sticky => 'w'})
}
TkLabel.new(self, :text => 'second label').grid({:column => 1, :row => 0, :sticky => 'w'})
end
end
那么...怎么了?从我看到的here 来看,应该可以使用ScrollableFrame#get_frame 检索帧,如下所示:
set a [$f getframe]
在 ruby 中,我会这样做:
sftest = scrollframe.get_frame
我的安装中不存在方法get_frame。但是我上面的 ruby 变体导致以下错误:
/usr/lib/ruby/1.9.1/tk.rb:215:in `class_eval': window name "frame" already exists in parent (RuntimeError)
我真的不知道为什么这个小脚本会抛出这个错误,所以我无法测试是否可以在这个小部件上配置高度和宽度。 如何为 ScrollableFrame 设置固定的高度和宽度?
更新:只要没有添加CheckButton,大小设置正确。我第一次添加CheckButton 时,ScrollableFrame 设置为CheckButton需要的大小。如何防止调整大小?
【问题讨论】:
标签: ruby frame tk scrolledwindow bwidget