我正在尝试编写一个绘图图形应用程序,但我需要用户输入(鼠标单击)和绘图区域/画布。我找到了这两个:http://zetcode.com/gui/rubyqt/introduction/和http://zetcode.com/gui/rubygtk/。我不关心它可以在哪个平台上运行。该项目将在Ruby上运行。感谢您的帮助或建议!
发布于 2013-05-28 19:09:38
试试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-28 19:05:18
您还可以尝试Tk,它具有Ruby的绑定(以及tcl、python、perl等其他几种语言)。有关概述和示例教程,请参阅tkdocs.com。有关绘制图形的信息,请参阅canvas小部件。
下面是来自该网站一个示例,它展示了如何在画布上交互式地绘制一条线:
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.mainloophttps://stackoverflow.com/questions/16790361
复制相似问题