Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scrolling multiple canvases

Status
Not open for further replies.

mitofi

Programmer
Jun 6, 2008
2
US
I have two canvases I am trying to have scrolled by the same horizontal scroll bar. I can get them to both react, but it seems, because the two canvases are not of the same width, the rate of scroll differs between the two - such that one canvas stops moving before the other. I need to them be in sync, as one is a graph of data and the other is an x-axis label. I am using grid for packing. I develop on a linux system.

I have also tried using 4 canvases instead of two: the graph, the main x-axis label, a small right and left portion. I did this to see if having them all the same scrollable width would make a difference, but no luck.

Any suggestions or ides?

Below is a dirty test script to demonstrate what it is I was trying to do.

Thanks!

------------------------------------------------

#! /bin/sh
# \
exec wish "$0" "$@"

proc BindView { which lists args } {
foreach l $lists {
eval {$l $which } $args
}
}

set mainw 200
set canvw 440
set sidew 30
set ht 25


set f [frame .f ]
set txt ""

scrollbar .f.scr -orient horizontal -command [list BindView xview [list .f.left .f.mid .f.right ] ]
#scrollbar .f.scr -orient horizontal -command [list BindView xview [list .f.left .f.right ] ]
scrollbar .f.scr2 -orient horizontal -command [list BindView xview [list .f.xc .f.midc ] ]


canvas .f.left -width $sidew -height $ht -highlightthickness 0 -borderwidth 0 \
-background red \
-xscrollcommand [list .f.scr set] \
-scrollregion "0 0 [expr $canvw - $mainw + $sidew ] $ht"
canvas .f.right -width $sidew -height $ht -highlightthickness 0 -borderwidth 0 \
-background blue \
-xscrollcommand [list .f.scr set] \
-scrollregion "0 0 [expr $canvw - $mainw + $sidew ] $ht"
canvas .f.mid -width $mainw -height $ht -highlightthickness 0 -borderwidth 0 \
-background white \
-xscrollcommand [list .f.scr set] \
-scrollregion "0 0 $canvw $ht"

canvas .f.midc -width $mainw -height $ht -highlightthickness 0 -borderwidth 0 \
-background pink \
-xscrollcommand [list .f.scr2 set] \
-scrollregion " 0 0 $canvw $ht "
canvas .f.xc -width [expr $mainw + ( 2 * $sidew) ] -height $ht \
-highlightthickness 0 -borderwidth 0 \
-background white \
-xscrollcommand [list .f.scr2 set] \
-scrollregion " 0 0 [expr $canvw + $sidew ] $ht "


grid .f.left -in $f -row 1 -column 1 -sticky nes
grid .f.mid -in $f -row 1 -column 2
grid .f.right -in $f -row 1 -column 3 -sticky nes
grid .f.scr -in $f -row 2 -column 2 -sticky news

grid .f.midc -in $f -row 3 -column 2
grid .f.xc -in $f -row 4 -column 1 -columnspan 3 -sticky news
grid .f.scr2 -in $f -row 5 -column 2 -sticky news

pack $f

set count 0
for { set ii 0 } { $ii <= $canvw } { incr ii 20 } {
set lx [expr $ii + $sidew ]
set rx [expr $ii - $mainw ]
set mx [expr $ii + $sidew ]
.f.mid create text $ii 4 -anchor n -text $count
.f.left create text $lx 4 -anchor n -text $count
.f.right create text $rx 4 -anchor n -text $count

.f.midc create text $ii 4 -anchor n -text $count
.f.xc create text $mx 4 -anchor n -text $count
incr count
}
 
Consider this form of the xview command:
Tk help said:
pathName xview scroll number what
This command shifts the view in the window left or right according to number and what. Number must be an integer. What must be either units or pages or an abbreviation of one of these. If what is units, the view adjusts left or right in units of the xScrollIncrement option, if it is greater than zero, or in units of one-tenth the window's width otherwise. If what is pages then the view adjusts in units of nine-tenths the window's width. If number is negative then information farther to the left becomes visible; if it is positive then information farther to the right becomes visible.

You could play with the rate of scroll of either or both of the canvii.

_________________
Bob Rashkin
 
For anyone who may want to know, here is what I ended up doing. It may be a variation of what was suggested.

-------------------------------------

#! /bin/sh
# \
exec wish "$0" "$@"

set vieww 200
set canvw 440
set sidew 30
set ht 25
set xscr_incr 10
set maxfrac [expr 1.0 - ( double($vieww) / double($canvw) ) ]
set maxfrac2 [expr 1.0 - ( double($sidew) / double($canvw - $vieww + $sidew) ) ]


proc BindView { which lists args } {
foreach l $lists {
eval {$l $which } $args
}
}
proc BindViewMore { sides which lists args } {
global maxfrac maxfrac2
set newargs [split $args ]
set u_p ""
set na_size [llength $newargs ]
if { $na_size >= 2 } {
set action [lindex $newargs 0 ]
set amount [lindex $newargs 1 ]

if { $na_size == 3 } {
set u_p [lindex $newargs 2 ]
set newamount [expr int( ($maxfrac2 / $maxfrac) * $amount ) ]
BindView $which $lists $action $amount $u_p
BindView $which $sides $action $newamount $u_p
} else {
BindView $which $lists $action $amount
set newamount [expr ( $amount / $maxfrac ) * $maxfrac2 ]
BindView $which $sides $action $newamount
}
}
}

proc move_canv { flag } {
global graph_mover twpaths

set ww .f.gc1
if { $flag == "press" } {
set graph_mover(xx) [winfo pointerx $ww ]
} elseif { $flag == "release" } {
set graph_mover(xx) -1
} elseif { $flag == "move" } {
set px [expr $graph_mover(xx) - [winfo pointerx $ww ] ]
set graph_mover(xx) [winfo pointerx $ww ]

.f.gc1 xview scroll $px units
.f.mid xview scroll $px units
.f.left xview scroll $px units
.f.right xview scroll $px units
}
}

set f [frame .f ]
set txt ""

scrollbar .f.scr -orient horizontal -command [list BindViewMore [list .f.left .f.right ] xview [list .f.mid .f.gc1 ] ] -background yellow
scrollbar .f.scrh -orient horizontal -background green


canvas .f.left -width $sidew -height $ht -highlightthickness 0 -borderwidth 0 \
-background red -xscrollincrement $xscr_incr \
-xscrollcommand [list .f.scrh set] \
-scrollregion "0 0 [expr $canvw - $vieww + $sidew ] $ht"
canvas .f.right -width $sidew -height $ht -highlightthickness 0 -borderwidth 0 \
-background blue -xscrollincrement $xscr_incr \
-xscrollcommand [list .f.scrh set] \
-scrollregion "0 0 [expr $canvw - $vieww + $sidew ] $ht"
canvas .f.mid -width $vieww -height $ht -highlightthickness 0 -borderwidth 0 \
-background white -xscrollincrement $xscr_incr \
-xscrollcommand [list .f.scr set] \
-scrollregion "0 0 $canvw $ht"
canvas .f.gc1 -width $vieww -height $ht -highlightthickness 0 -borderwidth 0 \
-background pink -xscrollincrement $xscr_incr \
-xscrollcommand [list .f.scr set] \
-scrollregion " 0 0 $canvw $ht "

grid .f.left -in $f -row 1 -column 1 -sticky nes
grid .f.mid -in $f -row 1 -column 2
grid .f.right -in $f -row 1 -column 3 -sticky nes
grid .f.scrh -in $f -row 2 -column 2 -sticky news
grid .f.scr -in $f -row 2 -column 2 -sticky news
grid .f.gc1 -in $f -row 0 -column 2

bind .f.gc1 <ButtonPress-1> "move_canv press"
bind .f.gc1 <ButtonRelease-1> "move_canv release"
bind .f.gc1 <B1-Motion> "move_canv move"

pack $f
raise .f.scr

set count 0
for { set ii 0 } { $ii <= $canvw } { incr ii 20 } {
set lx [expr $ii + $sidew ]
set rx [expr $ii - $vieww ]
.f.mid create text $ii 4 -anchor n -text $count
.f.gc1 create text $ii 4 -anchor n -text $count
.f.left create text $lx 4 -anchor n -text $count
.f.right create text $rx 4 -anchor n -text $count

incr count
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top