Hi
I'm trying to make an application but I'm having a small problem when I try to move a canvas inside a frame. The problem is that it shakes while I'm moving it and it moves slower than the mouse cursor.
If you can tell me wether my approach isn't right and point me on the right direction, or to say what I'm missing to do it right I would really thank you
The code is below
frame .mainFrame \
-width 400 \
-height 400 \
-bg black
canvas .mainFrame.canvas \
-yscrollcommand ".vertical set" \
-bg white \
-width 400 -height 400 \
-yscrollincrement 400 \
-insertwidth 1
grid .mainFrame.canvas \
-column 0 \
-row 0
scrollbar .vertical \
-orient vertical \
-command scrollCommand
grid .vertical \
-sticky news \
-row 0 -column 1
grid .mainFrame -column 0 -row 0
catch { tk_getOpenFile -initialdir ../imagens -multiple 1 } files
if { [ string compare $files "" ] != 0 } {
set cont 0
set blankSpace 0
foreach photoImage $files {
image create photo photo$cont -file $photoImage
.mainFrame.canvas create image 200 [expr 200 + $blankSpace] -image photo$cont
set blankSpace [expr $blankSpace + 400]
incr cont
}
}
.mainFrame.canvas configure -scrollregion "0 0 400 [expr [expr [llength [image names]] - 3] * 400]"
proc scrollCommand { args } {
eval ".mainFrame.canvas yview $args"
}
bind . <Button-5> {scrollCommand scroll 1 units}
bind . <Button-4> {scrollCommand scroll -1 units}
bind .mainFrame.canvas <B1-Motion> {drag %x %y}
bind .mainFrame.canvas <ButtonPress-1> {pushed %x %y}
bind .mainFrame.canvas <ButtonRelease-1> {released %x %y}
bind . <Key-c> {center}
proc center { } {
place .mainFrame.canvas -x 0 -y 0
}
set coordinates(firstX) 0
set coordinates(firstY) 0
proc pushed { x y } {
global coordinates
set coordinates(pushedX) $x
set coordinates(pushedY) $y
}
proc drag { x y } {
global coordinates
set coordinates(xDeslocation) [expr $x - $coordinates(pushedX)]
set coordinates(yDeslocation) [expr $y - $coordinates(pushedY)]
place .mainFrame.canvas \
-x [expr $coordinates(firstX) + $coordinates(xDeslocation)] \
-y [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}
proc released { x y } {
global coordinates
set coordinates(firstX) [expr $coordinates(firstX) + $coordinates(xDeslocation)]
set coordinates(firstY) [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}
I'm trying to make an application but I'm having a small problem when I try to move a canvas inside a frame. The problem is that it shakes while I'm moving it and it moves slower than the mouse cursor.
If you can tell me wether my approach isn't right and point me on the right direction, or to say what I'm missing to do it right I would really thank you
The code is below
frame .mainFrame \
-width 400 \
-height 400 \
-bg black
canvas .mainFrame.canvas \
-yscrollcommand ".vertical set" \
-bg white \
-width 400 -height 400 \
-yscrollincrement 400 \
-insertwidth 1
grid .mainFrame.canvas \
-column 0 \
-row 0
scrollbar .vertical \
-orient vertical \
-command scrollCommand
grid .vertical \
-sticky news \
-row 0 -column 1
grid .mainFrame -column 0 -row 0
catch { tk_getOpenFile -initialdir ../imagens -multiple 1 } files
if { [ string compare $files "" ] != 0 } {
set cont 0
set blankSpace 0
foreach photoImage $files {
image create photo photo$cont -file $photoImage
.mainFrame.canvas create image 200 [expr 200 + $blankSpace] -image photo$cont
set blankSpace [expr $blankSpace + 400]
incr cont
}
}
.mainFrame.canvas configure -scrollregion "0 0 400 [expr [expr [llength [image names]] - 3] * 400]"
proc scrollCommand { args } {
eval ".mainFrame.canvas yview $args"
}
bind . <Button-5> {scrollCommand scroll 1 units}
bind . <Button-4> {scrollCommand scroll -1 units}
bind .mainFrame.canvas <B1-Motion> {drag %x %y}
bind .mainFrame.canvas <ButtonPress-1> {pushed %x %y}
bind .mainFrame.canvas <ButtonRelease-1> {released %x %y}
bind . <Key-c> {center}
proc center { } {
place .mainFrame.canvas -x 0 -y 0
}
set coordinates(firstX) 0
set coordinates(firstY) 0
proc pushed { x y } {
global coordinates
set coordinates(pushedX) $x
set coordinates(pushedY) $y
}
proc drag { x y } {
global coordinates
set coordinates(xDeslocation) [expr $x - $coordinates(pushedX)]
set coordinates(yDeslocation) [expr $y - $coordinates(pushedY)]
place .mainFrame.canvas \
-x [expr $coordinates(firstX) + $coordinates(xDeslocation)] \
-y [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}
proc released { x y } {
global coordinates
set coordinates(firstX) [expr $coordinates(firstX) + $coordinates(xDeslocation)]
set coordinates(firstY) [expr $coordinates(firstY) + $coordinates(yDeslocation)]
}