From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2003-05-21T04:33:54+09:00 Subject: Re: problem deleting item on a TkCanvas In article , Phil Tomson wrote: >I've got a TkCanvas where I initially draw a vertical line at the far left >of the canvas. I've bound the left mouse button such that when pressed a >new vertical line is drawn at that x location, and the old line is >_supposed_ to be erased (deleted). But the old line is not being deleted. > >Here's the code: > >require 'tk' >class SigWave > def initialize(root,signame) > @frame = TkFrame.new(root).pack('anchor' => 'e','padx'=>1,'pady'=>1) > @name = TkLabel.new(@frame){text "#{signame}"}.pack('side'=>'left') > @canvas = TkCanvas.new(@frame) > @canvas.configure 'height'=> 20 > @canvas.pack('side'=>'right', 'fill'=>'x') > @cursor=TkcLine.new(@canvas, 1,0,1,20).fill('blue') > @canvas.bind('1',proc{|e| draw_cursor(e.x) }) > draw_cursor(1) > end > > def draw_cursor(x) > @canvas.delete(@cursor) #<= this seems to do nothing > @cursor=TkcLine.new(@canvas, x,0,x,20).fill('blue') > end >end > >root = TkRoot.new() {title "Waveform" } >SigWave.new(root,'signame') > >Tk.mainloop >#end of code > > >Am I doing the @canvas.delete correctly? > I figured it out. You've got to add a tag to the TkcLine, like: @cursor=TkcLine.new(@canvas, x,0,x,20,'tag'=>'cursor') Then the draw_cursor method becomes: def draw_cursor(x) @canvas.delete('cursor') @cursor=TkcLine.new(@canvas, x,0,x,20,'tag'=>'cursor') @cursor.fill('blue') end BTW: @cursor seems to be only an empty string. Phil