Tk의 문법
Tk의 문법은 매우 단순해서, 위젯과 경로, 옵션을 공백으로 구분해서 나열하기만 하면 됩니다. 위젯이란 GUI의 컴포넌트를 의미합니다.
widget .path ?-opt1 -opt2 -opt3 ...?
=>.path
.path subcmd ?-opt1 -opt2 -opt3 ...?
경로는 "." 으로 시작하는 경로명을 지정하며, "."은 루트를 나타냅니다. 옵션은 생략할 수 있습니다. 위젯 커맨드가 정상적으로 실행되면 경로명의 커맨드가 생성됩니다. (약간 객체지향적인?) 생성된 커맨드는 서브커맨드와 옵션이 있습니다. 그럼, 간단한 예제를 보겠습니다.
버튼
pack [button .b -text 버튼 -command exit]
set var 버튼
pack [button .b -textvariable var -command exit]
체크버튼
set 귤 1; set 바나나 1
foreach item {사과 귤 바나나} {
checkbutton .cb$item -text $item -variable $item
pack .cb$item -side left
}
라디오버튼
set choice 귤
foreach item {사과 귤 바나나} {
radiobutton .cb$item -text $item -variable choice -value $item
pack .cb$item -side left
}
라벨
pack [label .l -text 라벨]
set var 라벨
pack [label .l -textvariable var]
엔트리
set var 디폴트
pack [entry .e -textvariable var]
스핀박스 (Tk8.4~)
pack [spinbox .sb -from 1 -to 10 -textvariable var -increment 1 -wrap yes]
리스트박스
pack [listbox .l -exportselection 0]
.l insert end 사과 귤 바나나
.l selection set 0
# Tk8.3부터 -listvar 옵션 사용 가능
set var {사과 귤 바나나}
pack [listbox .l -exportselection 0 -listvar var]
.l selection set 0
텍스트
pack [text .t]
.t insert end "사과\n귤\n바나나"
스크롤바
pack [scrollbar .s -orient horizontal]
스케일
pack [scale .s -label 라벨 -from 0 -to 100 -length 100 \
-variable var -orient horizontal -tickinterval 50 -showvalue true]
프레임
pack [frame .f -bd 2 -relief groove -width 100 -height 50]
라벨프레임 (Tk8.4~)
pack [labelframe .f -text 라벨 -bd 2 -relief groove -width 100 -height 50]
분할윈도우 (Tk8.4~)
pack [panedwindow .p -orient horizontal]
label .p.l1 -text Left -width 10
label .p.l2 -text Right -width 10
.p add .p.l1 .p.l2
메시지
pack [message .m -justify left -text {긴 메시지입니다. \
한 줄에 쓸 수 없을 때는 백슬래시로 줄바꿈합니다.}]
캔버스
pack [canvas .c]
.c create oval 10 10 40 40 -fill red -width 4
.c create oval 30 10 60 40 -fill green -width 4
.c create oval 20 30 50 60 -fill blue -width 4
탑레벨
toplevel .main
wm transient .main [winfo toplevel [winfo parent .main]]
wm resizable .main no no
wm protocol .main WM_DELETE_WINDOW exit
pack [label .main.l -text "탑레벨"]
메뉴 (Tk8.0~)
menu .menu
.menu add cascade -label 파일 -menu .menu.file
.menu add cascade -label 편집 -menu .menu.edit
.menu add cascade -label 표시 -menu .menu.view
menu .menu.file -tearoff no
.menu.file add command -label 종료 -command exit
. configure -menu .menu
메뉴 버튼
frame .m
pack .m -side top -fill x
menubutton .m.file -text 파일 -menu .m.file.menu
menubutton .m.edit -text 편집
menubutton .m.view -text 표시
pack .m.file .m.edit .m.view -side left
menu .m.file.menu -tearoff 0
.m.file.menu add command -label 종료 -command exit
옵션 메뉴
tk_optionMenu .o var 사과 귤 바나나
pack .o
팝업 메뉴
menu .popup -tearoff no
.popup add command -label "열기..." -accelerator "Ctrl+O"
.popup add command -label "저장..." -accelerator "Ctrl+S"
.popup add separator
.popup add command -label "종료..." -accelerator "Ctrl+W" -command exit
bind . <3> { tk_popup .popup %X %Y }
메시지 박스
tk_messageBox -type ok -title 메시지박스 -icon info \
-message 메시지입니다.
다이얼로그 박스
after 10 {
wm withdraw .dlg
set x [expr [winfo rootx .] + 20]
set y [expr [winfo rooty .] + 20]
wm geometry .dlg +$x+$y
wm deiconify .dlg
wm resizable .dlg no no
wm protocol .dlg WM_DELETE_WINDOW {destroy .dlg}
.dlg.msg configure -text 라벨
}
tk_dialog .dlg 다이얼로그 {} {} 0 OK 취소
파일 오픈 다이얼로그
set types {
{ "Text" {.txt} }
}
set file [tk_getOpenFile -filetypes $types -title 열기]
파일 세이브 다이얼로그
set types {
{ "Text" {.txt} }
}
set file [tk_getSaveFile -filetypes $types -title 저장 -defaultextension .txt]
디렉터리 선택 다이얼로그 (Tk8.3~ )
set dir [tk_chooseDirectory -title 디렉터리]
컬러 선택 다이얼로그
set color [tk_chooseColor -title 컬러]