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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to resize canvas inside a frame?

Status
Not open for further replies.

tklearner

Programmer
Mar 10, 2003
3
US
Hi, I am a tk beginner, any help will be greatly apprecitated.
My problem is I have a frame which contain a canvas inside,
if I resize frame, the canvas did not resize to fill the
frame. How can I make canvas resize whenever the frame resize?
Thanks.

frame .f
canvas .f.c -width 200 -height 100 -bg beige -yscrollcommand {.f.vs set} -xscrollcommand {.f.hs set}
scrollbar .f.vs -command {.f.c yview}
scrollbar .f.hs -orient horizontal -command {.f.c xview}
grid .f.c .f.vs -sticky ns
grid .f.hs -sticky ew
pack .f

#.f.c create text 0 0 -anchor nw -text (0,0)
#.f.c create text 300 200 -anchor se -text (300,200)


set img [image create photo -file logo.gif]


.f.c create image 10 10 -anchor nw -image $img


.f.c config -scrollregion [.f.c bbox all]
 
In fact, you should resize the canvas and the frame will shrink and grow following the need.
This is due to the fact that the frame is just sized to its contained widgets (the children need is propagated to the master frame).
Code:
  frame .f -bg navy
  canvas .f.c -width 200 -height 100 -bg red
  pack .f .f.c
  update
  after 1000
  .f.c config -width 100 -height 200
If you really need to control the whole by the size of the frame, you need to ask the geometry manager to resize the children and not propagate.
Code:
  frame .f -width 200 -height 100 -bg navy
  pack propagate .f 0
  canvas .f.c -bg red
  pack .f
  pack .f.c -fill both -expand 1
  update
  after 1000
  .f config -width 100 -height 200
HTH

ulis
 
Thanks for response. My problem is when I click maximize button. The
canvas does not maximize even with your code.

 
Ah... This is another problem.
The pack and grid geometry managers can expand and shrink the children according to the space left in the parent.
With the pack manager what you want it is really easy:
Code:
  frame .f
  canvas .f.c -width 100 -height 100 -bg red
  pack .f .f.c -fill both -expand 1
-fill x expands and shrinks along the x-axis, -fill y along the y-axis and -fill both along the two axis.
-expand 1 tells the pack manager to expand and shrink the widget inside its parent.
You need to specify -expand 1 for both the frame and the canvas:
The frame is expanding and shrinking inside its parent: the root toplevel and the canvas is expanding and shrinking inside its parent: the frame.

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top