본문으로 바로가기

Tk의 레이블 프레임(label frame)

category 카테고리 없음 2024. 4. 30. 14:04

Tcl/Tk 8.4부터 추가된 labelframe 위젯을 소개합니다. 기본형은 -text 옵션으로 레이블을 지정하는 방식입니다.

pack [labelframe .f1 -text Label -pady 2 -padx 2]
 
set t 0
foreach str {Option1 Option2 Option3} {
        checkbutton .f1.b$t -text $str
        pack .f1.b$t -side top -fill x -pady 2
        incr t
}

 

레이블의 위치는 -labelanchor 옵션으로 변경할 수 있습니다. 기본 위치는 nw입니다. ne를 지정하면 색다른 분위기가 납니다.

pack [labelframe .f1 -text Label -pady 2 -padx 2 -labelanchor ne]
 
set t 0
foreach str {Option1 Option2 Option3} {
        checkbutton .f1.b$t -text $str
        pack .f1.b$t -side top -fill x -pady 2
        incr t
}

 

레이블 대신 -labelwidget 옵션을 사용하여 위젯을 지정할 수 있습니다. 아래는 레이블 대신 checkbutton을 사용한 예입니다.

pack [labelframe .f1 -pady 2 -padx 2]
checkbutton .f1.cb -text "Use this option." -variable lfdummy2 \
        -command "lfEnableButtons .f1" -padx 0
.f1 configure -labelwidget .f1.cb
 
set t 0
foreach str {Option1 Option2 Option3} {
        checkbutton .f1.b$t -text $str
        pack .f1.b$t -side top -fill x -pady 2
        incr t
}
 
proc lfEnableButtons {w} {
        foreach child [winfo children $w] {
                if {$child == "$w.cb"} continue
                if {$::lfdummy2} {
                        $child configure -state normal
                } else {
                        $child configure -state disabled
                }
        }
}
lfEnableButtons .f1

 

기본적으로 레이블 대신 어떤 위젯도 지정할수 있지만, 실용적인 것은 checkbutton과 message 정도일 겁니다. 아래는 레이블 대신 message를 사용한 예입니다. 길은 레이블은 자동적으로 접어주기 때문에 편리할 것입니다.

pack [labelframe .f1 -text Label -pady 2 -padx 2]
message .f1.m -text { This is long label } -justify left
.f1 configure -labelwidget .f1.m
 
set t 0
foreach str {Option1 Option2 Option3} {
        checkbutton .f1.b$t -text $str
        pack .f1.b$t -side top -fill x -pady 2
        incr t
}

 

labelframe을 잘 사용한다면 GUI의 표현력에 현격한 차이가 날 것이 틀림이 없습니다.