如何在 Ruby TK 中使用画布制作可滚动框架? [英] How to make a scrollable frame with canvas in Ruby TK?

查看:23
本文介绍了如何在 Ruby TK 中使用画布制作可滚动框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要滚动包含多个标签的框架.

由于框架不可滚动,所以我选择使用包含带有这些标签的框架的画布.

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.

如何设置框架不让它调整大小?使用 canvas.grid_propagate(false)?以及如何使画布可滚动?

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

我正在使用 ruby 2.3.3p222(2016-11-21 修订版 56859)[x64-mingw32]

使画布可滚动的工作代码是:

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)}

推荐答案

我不熟悉 Ruby 中的 Tk,也不熟悉 Ruby.但是您似乎只是通过网格几何管理器将框架放在画布顶部.在 Tcl/Tk 中,您需要将框架作为窗口项放在画布中.在 Internet 上的快速搜索似乎表明您需要使用 TkcWindow 类在 Ruby 中执行相同的操作.

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.

再说一次,我不懂 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)

无论如何,请查看 TkcWindow 类以了解如何进行.

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

这篇关于如何在 Ruby TK 中使用画布制作可滚动框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆