How to make a scrollable frame with canvas in Ruby TK?

这一生的挚爱 提交于 2021-01-29 07:20:19

问题


I need to scroll a frame that contains multiple labels.

Since frames aren't scrollable, so i've choosed to use a canvas that contains a frame with these labels in it.

But this would be the first time for me using a canvas, so i really don't know where to start, i ended up with this code:

canvas=TkCanvas.new(root) {grid :row =>0, :column =>0}
frame=TkLabelframe.new(canvas) {grid :row =>0, :column =>0}

scroll=Tk::Tile::Scrollbar.new(root) {orient 'vertical'; grid :row =>0, :column =>1, :sticky =>"wns"}
canvas.yscrollcommand proc {|*args| scroll.set(*args)}
scroll.command proc {|*args| canvas.yview(*args)}


x=0
5.times {
  lab=TkLabel.new(frame) {grid :row =>x, :column =>0, :sticky =>"w"}
  lab.text "Aaa..."
  x+=1
}

But i get the frame to resize for each label i'm gonna add, and so the canvas will never be scrollable.

How can i set the frame to not let it resize? Using canvas.grid_propagate(false)? And how can i make the canvas scrollable?

I'm using ruby 2.3.3p222 (2016-11-21 revision 56859) [x64-mingw32]

The working code to make the canvas scrollable is:

canvas=TkCanvas.new(root) {grid :row =>0, :column =>0}
frame=TkLabelframe.new(canvas) {grid :row =>0, :column =>0}
scroll=Tk::Tile::Scrollbar.new(root) {orient 'vertical'; grid :row =>0, :column =>1}
TkcWindow.new(canvas, 1, 1, :window=>frame, :anchor=>'nw')

canvas.configure(:scrollregion => "0 0 400 400")
canvas.grid_propagate(false)
canvas.yscrollcommand proc {|*args| scroll.set(*args)}
scroll.command proc {|*args| canvas.yview(*args)}

回答1:


I'm not familiar with Tk in Ruby, or Ruby for that matter. But you seem to just put the frame on top of the canvas via the grid geometry manager. In Tcl/Tk you need to put the frame as a window item in the canvas. A quick search of the internet seems to indicate that you would need to use the TkcWindow class to do the same thing in Ruby.

Again, I don't know Ruby, but I imagine the code should look something like this:

canvas=TkCanvas.new(root) {grid :row =>0, :column =>0}
frame=TkLabelframe.new(canvas)
TkcWindow.new(canvas, [0, 0], :window => frame)

In any case, look at the TkcWindow class for how to proceed.



来源:https://stackoverflow.com/questions/54245643/how-to-make-a-scrollable-frame-with-canvas-in-ruby-tk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!