From: Hidetoshi NAGAI Date: 2011-03-02T07:31:52+09:00 Subject: Re: tk canvas drag items From: Ignatz Hafflegab Subject: Re: tk canvas drag items Date: Tue, 1 Mar 2011 12:06:19 +0900 Message-ID: <70c564908d72fa5bff5bb1fd5b562698@ruby-forum.com> > What I'm looking for is an example that will confine a selected item > within the boundaries fixed-size canvas while the item is dragged. It's not difficult. Please select "A simple 2-D plot" on widget-demo, and press "Show Code" button on the plot-demo window. On the "Demo Code" window, please edit plotMove method like the following. ----------------------------------------------------- def plotMove (w, x, y) if x < 0 x = 0 else width = w.winfo_width x = width if x > width end if y < 0 y = 0 else height = w.winfo_height y = height if y > height end w.move 'selected', x - $plot['lastX'], y - $plot['lastY'] $plot['lastX'] = x $plot['lastY'] = y end ----------------------------------------------------- As you see, it checks whether the coordinates are within the canvas window. If you want control the region by canvas coordinates, you mark (on PlotDown method) and check coords by canvas coords. After editing, click "Rerun Demo" button on the editor window. And then, dragging the points on the restarted demo will be restricted by the canvas window size. -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) Department of Artificial Intelligence, Kyushu Institute of Technology