应用实例
创建弹出窗口
#!/bin/wish
proc popup_window { win {title "Popup Window"} {
toplevel $win -borderwidth 10 -width 600 -height 400
wm title $win $title
text $win.text
pack $win.text -fill both
}
popup_window .test_popup "Hello World"
最大化弹出窗口
proc maximize_toplevel {
if {([catch {package present Tk 8.3}] == 0) &&
[string equal $::tcl_platform(platform) windows]} {
wm state $toplevel zoomed
return
}
pack propagate $toplevel 0
update idletasks
wm geometry $toplevel +0+0
wm minsize $toplevel [winfo screenwidth $toplevel] [winfo screenheight $toplevel]
}
Modal Dialog
set w .dlg
toplevel $w ; # 创建对话框, ...
# 限制事件的发生范围
grab $w
# 指定为从属窗口
wm transient $w .
wm protocol $w WM_DELETE_WINDOW "grab release $w; destroy $w"
# 窗口放到最上层
raise $w
# 等待窗口结束
tkwait window $w
# destroy $w
更多实例
文件对话框 (打开/保存/选择)
set types {
{{Text Files} {.txt} }
{{TCL Scripts} {.tcl} }
{{C Source Files} {.c} TEXT}
{{GIF Files} {.gif} }
{{GIF Files} {} GIFF}
{{All Files} * }
}
set filename [tk_getOpenFile -filetypes $types]
if {$filename != ""} {
# Open the file ...
}
set filename [tk_getSaveFile -filetypes $types]
if {$filename != ""} {
# Write the file ...
}
set dir [tk_chooseDirectory -mustexist -initialdir ~ -title "Choose a directory"]
if {$dir != ""} {
# Process the directory
}