ulis, 2006-01-14. Voici une fenêtre entièrement dynamique et construite avec pack. Le challenge était, après grid de le faire avec un autre geometry manager.
Pourquoi
Pour comparer les mérites respectifs des geometry managers.
Comment
En proposant un script qui construit une fenêtre relativement complexe et redimensionnable avec le geometry manager de son choix.
Packings avec le crack : pack
# packings
package require Tk
proc menuBar {menues} \
{
frame .menuBar -bg white
set col 0
foreach menu $menues \
{
label .menuBar.$col -text $menu -bg white
pack .menuBar.$col -fill x -expand 0 -side left -padx 5 -pady 2
incr col
}
pack .menuBar -fill x -expand 0 -side top
}
proc toolBar {tools} \
{
frame .toolBar -bd 1 -relief ridge
set col 0
foreach tool $tools \
{
button .toolBar.$col -text $tool -bd 1 -relief flat -over raised -pady 5
pack .toolBar.$col -fill x -expand 0 -side left -padx 1 -pady 1
incr col
}
pack .toolBar -fill x -expand 0 -side top
}
proc middleArea {} \
{
frame .f
pack .f -fill both -expand 1 -side top
}
proc statusArea {areas} \
{
frame .f.statusArea -padx 10 -pady 4
canvas .f.statusArea.c -width 100 -height 0
pack .f.statusArea.c -fill x -side top
set row 0
foreach area $areas \
{
status $row $area
incr row
}
pack .f.statusArea -fill y -expand 0 -side left
}
proc displayArea {} \
{
frame .f.displayArea -bd 1 -relief groove -padx 4 -pady 4
text .f.displayArea.t -bd 0
scrollbar .f.displayArea.hs -orient horizontal
pack .f.displayArea.t -fill both -expand 1 -side top
pack .f.displayArea.hs -fill x -expand 0 -side bottom
pack .f.displayArea -fill both -expand 1 -side right
}
proc statusBar {} \
{
frame .statusBar -bd 1 -relief sunken
label .statusBar.l
pack .statusBar.l -fill x -expand 0
pack .statusBar -fill x -expand 0 -side bottom
}
proc status {row area} \
{
set color1 #606080
set color2 #8080c0
set color3 #d0d0c0
set a .f.statusArea.$row
foreach {action title} $area break
frame $a -padx 4 -pady 4 -bg $color1
frame $a.f1 -padx 5 -bg $color2 -pady 3
label $a.f1.l -text $title -anchor w -bg $color2
button $a.f1.b -width 2 -bg $color2 -bd 1 -highlightt 0
pack $a.f1.l -fill x -side left
pack $a.f1.b -side right -padx 1
frame $a.f2 -padx 10 -bg $color1
set _row 0
foreach link [lrange $area 2 end] \
{
label $a.f2.$_row -text $link -anchor w -bg $color1 -fg $color3
pack $a.f2.$_row -side top -pady 5
incr _row
}
pack $a.f1 -fill x -side top
pack $a.f2 -fill x -side top
$action $row
pack $a -fill x -side top -pady 5
}
proc shrink {row} \
{
set a .f.statusArea.$row
$a.f1.b config -text - -command [list enlarge $row]
pack forget $a.f2
}
proc enlarge {row} \
{
set a .f.statusArea.$row
$a.f1.b config -text v -command [list shrink $row]
pack $a.f2
}
wm title . "Packings"
wm protocol . WM_DELETE_WINDOW exit
menuBar [list File Edit Windows ?]
toolBar [list New Open Save]
set area1 [list enlarge "area 1" "action 1" "action 2" "action 3"]
set area2 [list shrink "area 2" "action 1" "action 2"]
set area3 [list enlarge "area 3" "action 1"]
middleArea
statusArea [list $area1 $area2 $area3]
displayArea
statusBar
update
wm minsize . [winfo width .] [winfo height .]Voir aussi
--
Les caractéristiques de pack
Pour une fenêtre simple, pack est le geometry manager le plus simple à utiliser. Il n'a pas toute la puissance de grid pour les fenêtres complexes. Il ne sait pas gérer le positionnement en absolu comme place.
Discussion
Dommage que mon challenge n'ait inspiré personne...
Moi j'étais partant, mais je n'ai pas eu le temps de le faire...
Lol ! Désolé. Peut-être un script plus élégant ? Ou avec place (là c'est pas facile) ?
Bien sûr, il s'agit d'exercices de styles car un vrai programme n'hésiterait pas à mélanger les geometry managers suivant les besoins.
Mon souhait est de voir fleurir des scripts élégants et des fenêtres esthétiques.
--
Catégorie Challenge | Catégorie Exemple