J
Josef Wolf
Hello!
I am trying to bind context-menus to a Tk canvas. The canvas itself should
have a context menu, giving the ability to create new objects. In addition,
every object should have its own context menu so that the object can be
modified/deleted. This is what I'm doing:
#! /usr/bin/ruby
require 'tk'
$-w = 1
root = TkRoot.new
canvas = TkCanvas.new.pack
# This is the context menu for the canvas to create new objects
#
menu=TkMenu.new
menu.add('command', 'label'=>'New Foo', 'command'=>proc{p "new foo"})
menu.add('command', 'label'=>'New Bar', 'command'=>proc{p "new bar"})
# canvas binding
canvas.bind("Button-3") { |e| evt=e; menu.popup(e.x_root, e.y_root) }
# Now create a new object and bind to it a context menu to modify/delete
# the object
#
rect = TkcRectangle.new(canvas, 0, 0, 50, 50, "fill"=>"white")
menu=TkMenu.new
menu.add('command', 'label'=>'Edit', 'command'=>proc{p "edit"})
menu.add('command', 'label'=>'Delete', 'command'=>proc{p "del"})
# object binding
canvas.itembind(rect, "Button-3") { |e| menu.popup(e.x_root, e.y_root) }
Tk.mainloop
Unfortunately, this don't work as desired. As soon as the binding to the
object is done, the canvas binding seems to be overridden. Even when
right-klicking on empty space in the canvas, the object's menu is invoked.
Any ideas what I am doing wrong here?
I am trying to bind context-menus to a Tk canvas. The canvas itself should
have a context menu, giving the ability to create new objects. In addition,
every object should have its own context menu so that the object can be
modified/deleted. This is what I'm doing:
#! /usr/bin/ruby
require 'tk'
$-w = 1
root = TkRoot.new
canvas = TkCanvas.new.pack
# This is the context menu for the canvas to create new objects
#
menu=TkMenu.new
menu.add('command', 'label'=>'New Foo', 'command'=>proc{p "new foo"})
menu.add('command', 'label'=>'New Bar', 'command'=>proc{p "new bar"})
# canvas binding
canvas.bind("Button-3") { |e| evt=e; menu.popup(e.x_root, e.y_root) }
# Now create a new object and bind to it a context menu to modify/delete
# the object
#
rect = TkcRectangle.new(canvas, 0, 0, 50, 50, "fill"=>"white")
menu=TkMenu.new
menu.add('command', 'label'=>'Edit', 'command'=>proc{p "edit"})
menu.add('command', 'label'=>'Delete', 'command'=>proc{p "del"})
# object binding
canvas.itembind(rect, "Button-3") { |e| menu.popup(e.x_root, e.y_root) }
Tk.mainloop
Unfortunately, this don't work as desired. As soon as the binding to the
object is done, the canvas binding seems to be overridden. Even when
right-klicking on empty space in the canvas, the object's menu is invoked.
Any ideas what I am doing wrong here?