본문으로 바로가기

Tk의 레이아웃(layout)

category 카테고리 없음 2025. 8. 14. 21:39

위젯을 배치하는 방법에는 아래의 3가지가 있습니다. 각각 장단점이 있으니, 용도에 맞게 사용하시면 됩니다.

  • place 방식
    위젯을 좌표로 배치합니다. 오래된 배치 방식입니다.
  • pack 방식
    위젯을 방향(동서남북) 기준으로 배치합니다. 일반적인 배치 방식입니다.
  • grid 방식
    위젯을 격자 형태로 배치합니다. 새로운 배치 방식입니다.

place 방식

윈도우 크기 조절(리사이즈)에 대응하기 힘듭니다. 픽셀 단위로 배치하므로 스크립트 작성 시 상당히 피곤합니다.

text .t -yscroll {.sv set} -xscroll {.sh set} -wrap none  
place .t -x 0 -y 0 -width 100 -height 100  
scrollbar .sv -command {.t yview}  
place .sv -x 100 -y 0 -height 100  
scrollbar .sh -orient horizontal -command {.t xview}  
place .sh -x 0 -y 100 -width 110  
. configure -width 115 -height 115

pack 방식

윈도우 크기 조절(리사이즈)에 대응 가능합니다. 크기를 조절하면 text 위젯이 그에 맞춰 따라갑니다. 오른쪽 아래 처리가 좀 부족합니다.

pack [frame .f] -fill both -expand 1  
text .t  -width 15 -height 7 -yscroll {.sv set} -xscroll {.sh set} -wrap none  
pack .t -side left -fill both -expand 1 -in .f  
scrollbar .sv -command {.t yview}  
pack .sv -side right -fill y -in .f  
scrollbar .sh -orient horizontal -command {.t xview}  
pack .sh -side top -fill x

grid 방식

윈도우 크기 조절(리사이즈)에 대응 가능합니다. 크기를 조절하면 text가 그에 맞춰 따라갑니다. 코드가 다소 길어지고 복잡해지는 단점이 있습니다.

text .t  -width 15 -height 7 -yscroll {.sv set} -xscroll {.sh set} -wrap none
scrollbar .sv -command {.t yview}
scrollbar .sh -orient horizontal -command {.t xview}
grid .t -in . -row 0 -column 0 -sticky nsew
grid .sv -in . -row 0 -column 1 -sticky ns
grid .sh -sticky we
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1