From: nagai@... Date: 2001-11-11T23:22:30+09:00 Subject: [ruby-talk:24876] Re: Ruby-Tk; feature/bug/misunderstanding? mouse-location during when a key is pressed in the presence of TkMenubutton Hi, From: Armin Roehrl Subject: [ruby-talk:24810] Ruby-Tk; feature/bug/misunderstanding? mouse-location during when a key is pressed in the presence of TkMenubutton Date: Sat, 10 Nov 2001 07:42:26 +0900 Message-ID: <0111092342181W.03764@linus> armin> I think the following is best explained with code. armin> Can s.b. reproduce that error? I'm wondering that it is what you are aiming. The menubar area is out of canvas. Do you really want to generate canvas objects on such area? If you want to do binding for keyboard events, you should set focus on the target widget. Therefore, 1st answer for the problem is, "set forcus on the canvas, and bind to the canvas for keyboard events." --------------------------------------- canvas.focus canvas.bind('d', proc{|x,y| TkcRectangle.new(canvas, x,y,x-5,y-5)}, '%x %y') --------------------------------------- If you don't want to set focus on the canvas widget for some reason, one of the ansers was shown by some people. And, I'll show the other. The problem is that size of the canvas widget doesn't match to size of the root widget which gets keyboard events. Then, the answer is to make those sizes equal. It needs a little tricky way. That is to use two geometry managers, 'pack' and 'place', on the root widget. I'll show two variations for your 2nd example. --------------------------------------- require 'tk' root = TkRoot.new root.geometry '400x300' canvas = TkCanvas.new.place('relwidth'=>1.0, 'relheight'=>1.0) menuline = TkFrame.new(root, 'relief'=>'raised', 'borderwidth'=>2) \ .pack('side'=>'top', 'fill'=>'x') TkMenubutton.new(menuline) { |mb| text "Zoom" underline 0 menu TkMenu.new(mb) { add 'command', 'label' => 'Zoom in', 'underline' => 5, 'command' => proc { p "in"; } add 'command', 'label' => 'Zoom out', 'underline' => 5, 'command' => proc { p "out" } } pack('side' => 'left', 'padx' => '1m') } root.bind('d', proc{|x,y| TkcRectangle.new(canvas, x,y,x-5,y-5)}, '%x %y') f = TkFrame.new(root, 'relief'=>'raised', 'borderwidth'=>2) \ .pack('side'=>'bottom', 'fill'=>'x') TkButton.new(f, 'text'=>'EXIT', 'command'=>proc{exit}).pack('side'=>'right') Tk.mainloop --------------------------------------- --------------------------------------- require 'tk' root = TkRoot.new canvas = TkCanvas.new.pack('fill'=>'both', 'expand'=>'true') menuline = TkFrame.new(root, 'relief'=>'raised', 'borderwidth'=>2) \ .place('anchor'=>'nw', 'relx'=>0.0, 'rely'=>0.0, 'relwidth'=>1.0) TkMenubutton.new(menuline) { |mb| text "Zoom" underline 0 menu TkMenu.new(mb) { add 'command', 'label' => 'Zoom in', 'underline' => 5, 'command' => proc { p "in"; } add 'command', 'label' => 'Zoom out', 'underline' => 5, 'command' => proc { p "out" } } pack('side' => 'left', 'padx' => '1m') } root.bind('d', proc{|x,y| TkcRectangle.new(canvas, x,y,x-5,y-5)}, '%x %y') f = TkFrame.new(root, 'relief'=>'raised', 'borderwidth'=>2) \ .place('anchor'=>'sw', 'relx'=>0.0, 'rely'=>1.0, 'relwidth'=>1.0) TkButton.new(f, 'text'=>'EXIT', 'command'=>proc{exit}).pack('side'=>'right') Tk.mainloop --------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)