pack [button .b -text "The Button" -command exit] wm overrideredirect . 1 bind .b <1> { set iX0 [expr %X-[winfo rootx .b]] set iY0 [expr %Y-[winfo rooty .b]] set bMoved 0 } bind .b <B1-Motion> { wm geometry . +[expr %X-$iX0]+[expr %Y-$iY0] set bMoved 1 } bind .b <ButtonRelease-1> { if { $bMoved } break }
Lars H: The following demonstrates one way of dragging a canvas -- change which part of the coordinate system is visible in the window, by pressing and dragging button 1.
pack [canvas .c] -expand 1 -fill both .c create text 10 10 -anchor nw -text "Hello world!" .c create line {-100 0 100 0} -arrow last .c create line {0 -100 0 100} -arrow last bind .c <ButtonPress-1> {%W scan mark %x %y} bind .c <B1-Motion> {%W scan dragto %x %y 1}This is particularly useful with quick hacks, where one displays some piece of graphic in a canvas and doesn't know for sure whether all of it fits in the visible part of the canvas.
dzach: And if one wants to limit dragging to the occupied area of a canvas, then:
.c configure -scrollregion [.c bbox all]does it, provided it is called after the items have been drawn.[Next up: exhibit dragging an item around a canvas. Check with CL if you're in a hurry.]