Problem deleting item on a TkCanvas

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?

Phil

In article bacl9402479@enews4.newsguy.com,

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

···

Phil Tomson ptkwt@shell1.aracnet.com wrote:

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

use @cursor.delete instead ?

@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?

Phil

I would do a @cursor.coords(…);Tk.update instead. I noticed some
memory problems when creating/deleting a big number of canvas items with
ruby.

regards,
jf