字符编码是字符在计算机内部的表示。字体是字符在Tk组件中显示时的风格。
要字符正确显示,首先要编码正确,其次要字体正确。一个程序中字符编码通常只有一种,字体则允许同时有多种。
button .btn -text "你好" -font "宋体 15" pack .btn
为每一个组件指定字体太麻烦,全局指定的方法是在程序开始时加上
option add *font "Helvetica 10" # 或者 option add *font "宋体 15"
Tk按照如下5种规则按顺序尝试解析字体。
字体的名字由命令 font create
指定或返回。
比如:-font [font create -family 宋体 -size 13 -underline true]
比如Windows平台上就有下面几种字体:ansi、ansifixed、device、oemfixed、system、systemfixed
其格式为:family ?size? ?style? ?style ...?
比如:-font “宋体 13 italic”
可以使用的格式有 bold、italic、normal、overstrike、roman
其形如://-foundry-family-weight-slant-setwidth-addstyle-pixel-point-resx-resy-spacing-width-charset-encoding//
比如:-font "-family 宋体 -size 13 -underline true"
proc firstfont {fontlist default} { set avail {} foreach f [font families] { lappend avail [string tolower $f] } foreach try $fontlist { if {[lsearch $avail [string tolower $try]] != -1} { return $try } } return $default } set fontfam [firstfont {{Palatino Linotype} {Times New Roman}} \ Times] font create body -family $fontfam -size 11 font create title -family $fontfam -size 16
switch $tcl_platform(platform) "unix" { set font times-roman-10 } "macintosh" { set font {-size 9} } "windows" { set font {-family {MS Sans Serif} -size 7} }