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!

canvas + grid + scrolling question

Status
Not open for further replies.

smugindividual

Programmer
Apr 14, 2003
104
US
Whats the trick/option on the canvas widget that will keep it from growing in size when I pack or grid items that will exceed the size. Basically I want a scrolling canvas that stays the size I specify. Its easy with text or listbox widgets, why is canvas giving me problems?


My program is much larger but the following code illustrates my issue.

frame .f -height 5
canvas .f.can -height 5
scrollbar .f.sb -command ".f.can yview"
.f.can configure -yscrollcommand ".f.sb set"

for { set i 0 } { $i < 35 } { incr i } {
for { set j 0 } { $j < 5 } { incr j } {
label .f.can.$i$j -text &quot;$i-$j&quot;
grid .f.can.$i$j -row $i -column $j
}
}
pack .f.can .f.sb -side left -fill y
pack .f
 
Creating labels as children of the canvas don't make them text items of the canvas. They still are outside the canvas (but can overlap it).
What you want is to create text items of the canvas.
But you lose the grid capacity because grid is a geometry manager of widgets, not of text items of a canvas.
Here is a working code:
Code:
  set dx 40
  set dy 16
  set ww [expr {5 * $dx}]
  set hh [expr {5 * $dy}]
  canvas .can -width $ww -height $hh -yscrollcommand {.sb set}
  scrollbar .sb -command {.can yview}
  set y 0
  for {set i 0} {$i < 35} {incr i}   {
    set x 0
    for {set j 0} {$j < 5} {incr j}     {
      .can create text $x $y -anchor nw -tags tag$i-$j -text &quot;$i-$j&quot;
      incr x $dx
    }
    incr y $dy
  }
  pack .can .sb -side left -fill y
  .can config -scrollregion [.can bbox all]
I think you'll want to compute the width and the height of the text items (hint: use the bbox operation of the canvas).

Good luck

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top