Comment choisir son geometry manager pour faire un bon placement ?
Je ne sais absolument pas répondre à la question, mais cette page devrait apporter quelques éléments de réponse.
Généralités
place ne redimensionne pas. Il faut donc le faire à la main.
A mon avis
Sauf cas évidents, il faut utiliser grid pour structurer son interface. Chaque cellule pouvant recevoir un frame qui permettra d'utiliser à nouveau grid (ou pack s'il est plus approprié).
Il faut utiliser place quand on ne peut faire autrement (déplacement d'un widget à la souris, par exemple).
Le script
À chaque geometry manager correspond une procédure qui permet de créer une des fenêtres ci-dessus.
Les fenêtres sont redimensionnables. La dernière procédure est nécessaire pour redimensionner avec place.
Le script se termine par une invocation des différentes procédures pour créer les trois fenêtres.
# the pack way
# needs frames
# resizing is very easy
proc packWay {} \
{
catch { destroy .pack }
toplevel .pack
wm protocol .pack WM_DELETE_WINDOW exit
frame .pack.f1
foreach i {1 2 3} \
{ button .pack.f1.b($i) -width 6 -text "pack $i" }
canvas .pack.c -bd 2 -relief sunken \
-width 200 -height 100
frame .pack.f2
foreach i {1 2} \
{ checkbutton .pack.f2.cb($i) -text "pack $i" }
pack .pack.f1
pack .pack.c -fill both -expand 1
pack .pack.f2
pack .pack.f1.b(1) .pack.f1.b(2) .pack.f1.b(3) \
-side left -padx 10 -pady 10
pack .pack.f2.cb(1) .pack.f2.cb(2) \
-side left -padx 15 -pady 10
}
# the grid way
# needs frames
# resizing is cumbersome
proc gridWay {} \
{
catch { destroy .grid }
toplevel .grid
wm protocol .grid WM_DELETE_WINDOW exit
frame .grid.f1
foreach i {1 2 3} \
{ button .grid.f1.b($i) -width 6 -text "grid $i" }
canvas .grid.c -bd 2 -relief sunken \
-width 200 -height 100
frame .grid.f2
foreach i {1 2} \
{ checkbutton .grid.f2.cb($i) -text "grid $i" }
grid .grid.f1
grid .grid.c -sticky nsew
grid .grid.f2
grid .grid.f1.b(1) .grid.f1.b(2) .grid.f1.b(3) \
-sticky ew -padx 10 -pady 10
grid .grid.f2.cb(1) .grid.f2.cb(2) \
-sticky ew -padx 15 -pady 10
# resizing
grid rowconfigure .grid 1 -weight 1
grid columnconfigure .grid 0 -weight 1
}
# the place way
# doesn't need frames
# resizing is very convoluted
proc placeWay {} \
{
catch { destroy .place }
toplevel .place
wm protocol .place WM_DELETE_WINDOW exit
foreach i {1 2 3} \
{
button .place.b($i) -width 6 -text "place $i"
place .place.b($i) -anchor n \
-relx [expr {$i * 0.25}] -rely 0 -y 10
}
canvas .place.c -bd 2 -relief sunken \
-width 200 -height 100
foreach i {1 2} \
{
checkbutton .place.cb($i) -text "place $i"
place .place.cb($i) -anchor s \
-relx [expr {$i * 0.33}] -rely 1.0 -y -10
}
place .place.c -relx 0.5 -rely 0.5 -anchor center
update
set w2 [winfo reqwidth .place.c]
set width [expr {0 + $w2 + 0}]
set ::h1 [winfo height .place.b(1)]
set ::h2 [winfo height .place.c]
set ::h3 [winfo height .place.cb(1)]
set height \
[expr {10 + $::h1 + 10 + $::h2 + 10 + $::h3 + 10}]
# sizing
wm geometry .place ${width}x${height}
# resizing
bind .place <Configure> placeResize
}
# resizing, the place way
set ::resizing 0
proc placeResize {args} \
{
if {$::resizing} { return }
set ::resizing 1
update
set width [winfo width .place]
# why I need to subtract 8 ?
set w2 [expr {$width - 8}]
set height [winfo height .place]
# why I need to subtract 8 ?
set h2 [expr {$height - 10 - 10 - 10 - 10 - $::h1 - $::h3 - 8}]
.place.c config -width $w2 -height $h2
set ::resizing 0
}
packWay
gridWay
placeWay
foreach manager {pack grid place} \
{
catch \
{
focus -force .$manager
raise .$manager
}
}Voir aussi
Discussion
09-01-2009, Création, ulis
20-08-2012, Corrections mineures, Stéphane Aulery
[ Catégorie Exemple | Catégorie Interface utilisateur | Catégorie Cours | Catégorie Tutoriel ]