require 'tk' x1 = TkVariable.new y1 = TkVariable.new x2 = TkVariable.new y2 = TkVariable.new TkFrame.new{|f| TkLabel.new(f,'text'=>'x1: ').pack('side'=>'left') TkEntry.new(f,'width'=>8, 'textvariable'=>x1 ).pack('side'=>'left') TkLabel.new(f,'text'=>' y1: ').pack('side'=>'left') TkEntry.new(f,'width'=>8, 'textvariable'=>y1 ).pack('side'=>'left') }.pack TkFrame.new{|f| TkLabel.new(f,'text'=>'x2: ').pack('side'=>'left') TkEntry.new(f,'width'=>8, 'textvariable'=>x2 ).pack('side'=>'left') TkLabel.new(f,'text'=>' y2: ').pack('side'=>'left') TkEntry.new(f,'width'=>8, 'textvariable'=>y2 ).pack('side'=>'left') }.pack c = TkCanvas.new(nil, 'background'=>'white' ) TkFrame.new{|f| TkButton.new(f,'text'=>'Plot', 'command'=>proc{ item = TkcOval.new(c, x1.to_i, y1.to_i, x1.to_i + 10, y1.to_i + 10, 'fill'=>'red'); item.addtag 'grph' }).pack('side'=>'left') TkButton.new(f,'text'=>'Line', 'command'=>proc{ item = TkcLine.new(c, x1.to_i, y1.to_i, x2.to_i, y2.to_i, 'width'=>2 ); item.addtag 'grph' }).pack('side'=>'left') TkButton.new(f,'text'=>'Rectangle', 'command'=>proc{ item = TkcRectangle.new(c, x1.to_i, y1.to_i, x2.to_i, y2.to_i, 'width'=>2 ); item.addtag 'grph' }).pack('side'=>'left') TkButton.new(f,'text'=>'Oval', 'command'=>proc{ item = TkcOval.new(c, x1.to_i, y1.to_i, x2.to_i, y2.to_i, 'width'=>2 ); item.addtag 'grph' }).pack('side'=>'left') TkButton.new(f,'text'=>'EXIT', 'command'=>proc{exit}).pack('side'=>'left') }.pack c.pack # c.itembind('grph','Any-Enter',proc{c.itemconfigure 'current','fill','blue'}) # c.itembind('grph','Any-Leave',proc{c.itemconfigure 'current','fill','red'}) c.itembind('grph','1',proc{|x,y| plotDown c, x, y }, "%x %y") c.itembind('grph', 'B1-Motion',proc{|x,y| plotMove c, x, y }, "%x %y") $plot = {'lastX'=>0, 'lastY'=>0 } def plotDown (w, x, y) w.dtag 'selected' w.addtag_withtag 'selected', 'current' w.raise 'current' $plot['lastX'] = x $plot['lastY'] = y end def plotMove (w, x, y) w.move 'selected', x - $plot['lastX'], y - $plot['lastY'] $plot['lastX'] = x $plot['lastY'] = y end Tk.mainloop