我正在尝试编写绘图应用程序,但我需要用户输入(鼠标点击)和绘图区域/画布。我找到了这个 2:http: //zetcode.com/gui/rubyqt/introduction/和http://zetcode.com/gui/rubygtk/。我不在乎它可以在哪个平台上运行。该项目将在 Ruby 上进行。感谢您的任何帮助或建议!
问问题
407 次
2 回答
2
您也可以尝试 Tk,它具有 Ruby 的绑定(除了其他几种语言,如 tcl、python、perl)。请参阅tkdocs.com以获取概述和示例教程。要绘制图表,请参阅画布小部件。
这是该网站上的一个示例,它显示了如何在画布上以交互方式绘制线条:
require 'tk'
root = TkRoot.new()
@canvas = TkCanvas.new(root)
@canvas.grid :sticky => 'nwes', :column => 0, :row => 0
TkGrid.columnconfigure( root, 0, :weight => 1 )
TkGrid.rowconfigure( root, 0, :weight => 1 )
@canvas.bind( "1", proc{|x,y| @lastx = x; @lasty = y}, "%x %y")
@canvas.bind( "B1-Motion", proc{|x, y| addLine(x,y)}, "%x %y")
def addLine (x,y)
TkcLine.new( @canvas, @lastx, @lasty, x, y )
@lastx = x; @lasty = y;
end
Tk.mainloop
于 2013-05-28T11:05:18.470 回答
2
Try QtRuby - Qt 功能是最全面的,IMO。
以下是如何跟踪坐标的示例:
require 'Qt4'
class MyWindow < Qt::Widget
def initialize
super
move 300, 300
setFixedSize(500, 500)
@label = Qt::Label.new(self)
@layout = Qt::VBoxLayout.new
@graphics = Qt::GraphicsScene.new(-100, -100, 400, 200)
@gv = Qt::GraphicsView.new(@graphics, self)
@label.show
@gv.show
@layout.add_widget(@gv, 0, Qt::AlignCenter)
@layout.add_widget(@label, 0, Qt::AlignCenter)
setLayout(@layout)
show
end
def mousePressEvent(e)
@mousePos = e.pos
@label.setText("x: #{@mousePos.x}, y: #{@mousePos.y}")
end
end
Qt::Application.new(ARGV) do
MyWindow.new
exec
end
不是最好的风格,但它会做一般的理解。
如果你想手动画线,Qt 已经有了这样的功能。此外,Qt 有一个漂亮的社区和文档:example
于 2013-05-28T11:09:37.890 回答