下面的 Tk 脚本建立一个界面,类似于:
#!/usr/bin/wish -f wm title . "gui_quads" label .msg -text "Solution of a x^2 + b x + c = 0" pack .msg -padx 5 -pady 3 -ipadx 5 -ipady 5 -fill x frame .f pack .f -padx 5 -ipadx 5
选项 -padx、-pady、-ipadx 和 -ipady 接受的值依照象素数或长度(单位: m 毫米,c 厘米)来指定。
在从属(slave)组件被包装进框架的时候,选项 -padx 和 -pady 告诉打包器(packer)在水平和垂直的方向上允许从属组件之间有指定的空间。这被称做外部填充。
在从属窗口被包装进主窗口的之前,选项 -ipadx 和 -ipady 告诉打包器( packer)在水平和垂直的方向上按给定的值放大(enlarge)从属窗口。这被称做内部填充。
外部填充影响在父组件中的兄弟组件之间的空间。内部填充放大一个从属组件的大小,超过由几何管理器计算出的大小(例如,一个标签组件变得比它显示标签字符串所需的最小宽度大)。
entry .f.a -relief sunken ;#for coefft. of x**2
label .f.x2 -text "x^2 + "
entry .f.b -relief sunken ;#for coefft. of x
label .f.x -text " x + "
entry .f.c -relief sunken ;# for constant term
label .f.rhs -text " = 0"
pack .f.a .f.x2 .f.b .f.x .f.c .f.rhs -in .f -side left \
-padx 3 -pady 3 -ipadx 2 -ipady 2
frame .zeros
pack .zeros
frame .zeros.base1 -bg red
frame .zeros.base2 -bg pink
pack .zeros.base1 -in .zeros -padx 5 -pady 5 -side top
pack .zeros.base2 -in .zeros -padx 5 -pady 5 -side top
label .zeros.x1 -text "x1 = "
label .zeros.x1val -bg yellow ;#to display the value of first real root
;# or real part of the complex roots
label .zeros.x2 -text "x2 = "
label .zeros.x2val -bg yellow ;#to display the value of second real root
;# or imaginary part of the complex roots
pack .zeros.x1 .zeros.x1val -side left -in .zeros.base1 -padx 5 -pady 5
pack .zeros.x2 .zeros.x2val -side left -in .zeros.base2 -padx 5 -pady 5
#
frame .info
pack .info
frame .info.dum
pack .info.dum -side left
set w .info.dum
label $w.disc ;#label to display the discriminant
label $w.type ;#label to display the type of the roots (real or complex)
pack $w.disc -padx 5 -pady 5
pack $w.type -padx 5 -pady 5
#
foreach e {.f.a .f.b .f.c} {
bind $e {invokeQuads}
}
#
#
# buttons
#
frame .bf
pack .bf -padx 5 -pady 5 -ipadx 4 -ipady 4 -fill x
button .bf.quit -text Quit -command {exit} ;#exit button to quit
button .bf.clear -text Clear -command clearEntries ;#Resets the entry fields
button .bf.solve -text Solve -command invokeQuads ;#calls "quads"
pack .bf.quit .bf.clear .bf.solve -side right \
-padx 5 -pady 5 -ipadx 3 -ipady 3
focus .f.a ;#set keyboard focus into first entry widget
过程 invokeQuads 和 clearEntries 在下一步中描述。