0%

Python Tkinter 组件外观

组件样式

所有的Tkinter标准组件都提供了最基础的样式选项,这些选项可以用来修改诸如颜色、字体和其他显示信息。

颜色

大多数组件允许你指定组件和文本的颜色,可以使用 backgroundforeground 选项。为了指定颜色,可以使用颜色的名字,比如red,或者RGB参数,比如FF0000。

颜色名

Tkinter 包括一个颜色数据库,它将颜色名映射到相应的 RGB 值。这个数据库包括了通常的名称如 Red,
Green, Blue, Yellow, 和 LightBlue,也可使用外来的如 Moccasin,PeachPuff 等等。

在 X window系统上,颜色名由 X server 定义。你能够找到 一个名为 xrgb.txt 的文件,它包含了一个由颜色名和相应
RGB 值组成的列表。在 Windows 和 Macintosh 系统上,颜色名表内建于 Tk 中。

在 Windows 下,你可以使用 Windows 系统颜色(用户可以通过控制面板来改变这些颜色):

SystemActiveBorder, SystemActiveCaption, SystemAppWorkspace, SystemBackground, SystemButtonFace, SystemButtonHighlight, SystemButtonShadow, SystemButtonText, SystemCaptionText, SystemDisabledText, SystemHighlight, SystemHighlightText, SystemInactiveBorder, SystemInactiveCaption, SystemInactiveCaptionText, SystemMenu, SystemMenuText, SystemScrollbar, SystemWindow, SystemWindowFrame, SystemWindowText.

在 Macintosh 上,下面的系统颜色是有效的:

SystemButtonFace, SystemButtonFrame, SystemButtonText, SystemHighlight, SystemHighlightText, SystemMenu, SystemMenuActive, SystemMenuActiveText, SystemMenuDisabled, SystemMenuText, SystemWindowBody.

颜色名是大小写不敏感的。许多颜色(并不是所有的颜色)名词与词之间有无格都有效。例如”lightblue”, “light blue”, 和”Light Blue”都是同一颜色。

RGB 格式

如果你需要显式地指定颜色名,你可以使用如下格式的字符串

1
#RRGGBB

RR, GG, BB 分别是 red,green 和 blue 值的十六进制表示。下面的例子演示了如何将一个颜色三元组转换 为 一个 Tk 颜色格式:

1
tk_rgb = "#%02x%02x%02x" % (128, 192, 200)

Tk 也支持用形如”#RGB”和”rrrrggggbbbb”去分别指定16和65536程度之间的值。

你可以使用窗口部件的 winfo_rgb 方法来将一个代表颜色的字符串(名字或 RGB 格式)转换为一个三元组:

1
2
rgb = widget.winfo_rgb("red")
red, green, blue = rgb[0]/256, rgb[1]/256, rgb[2]/256

注意 winfo_rgb 返回16位的 RGB 值,范围在065535之间。要将它们映射到更通用的0255范围内,你必
须将每个值都除以256(或将它们向右移8位)。

字体

窗口部件允许你显示文本和指定所使用的字体。所有的窗口部件都提供了合理的默认值,你很少需要去为简单 元素如标签和按钮指定字体。

字体通常使用 font 窗口部件选项指定。Tkinter 支持一定数量的不同字体描述类型:

  • Font descriptors
  • User-defined font names
  • System fonts
  • X font descriptors

Tk8.0以前的版本仅X font描述被支持。
Font descriptors #

Starting with Tk 8.0, Tkinter supports platform independent font descriptors. You can specify a font as tuple containing a family name, a height in points, and optionally a string with one or more styles. Examples:

(“Times”, 10, “bold”)
(“Helvetica”, 10, “bold italic”)
(“Symbol”, 8)

To get the default size and style, you can give the font name as a single string. If the family name doesn’t include spaces, you can also add size and styles to the string itself:

“Times 10 bold”
“Helvetica 10 bold italic”
“Symbol 8”

Here are some families available on most Windows platforms:

Arial (corresponds to Helvetica), Courier New (Courier), Comic Sans MS, Fixedsys, MS Sans Serif, MS Serif, Symbol, System, Times New Roman (Times), and Verdana:

Note that if the family name contains spaces, you must use the tuple syntax described above.

The available styles are normal, bold, roman, italic, underline, and overstrike.

Tk 8.0 automatically maps Courier, Helvetica, and Times to their corresponding native family names on all platforms. In addition, a font specification can never fail under Tk 8.0; if Tk cannot come up with an exact match, it tries to find a similar font. If that fails, Tk falls back to a platform-specific default font. Tk’s idea of what is “similar enough” probably doesn’t correspond to your own view, so you shouldn’t rely too much on this feature.

Tk 4.2 under Windows supports this kind of font descriptors as well. There are several restrictions, including that the family name must exist on the platform, and not all the above style names exist (or rather, some of them have different names).

Font names

In addition, Tk 8.0 allows you to create named fonts and use their names when specifying fonts to the widgets.

The tkFont module provides a Font class which allows you to create font instances. You can use such an instance everywhere Tkinter accepts a font specifier. You can also use a font instance to get font metrics, including the size occupied by a given string written in that font.

tkFont.Font(family=”Times”, size=10, weight=tkFont.BOLD)
tkFont.Font(family=”Helvetica”, size=10, weight=tkFont.BOLD,
slant=tkFont.ITALIC)
tkFont.Font(family=”Symbol”, size=8)

If you modify a named font (using the config method), the changes are automatically propagated to all widgets using the font.

The Font constructor supports the following style options (note that the constants are defined in the tkFont module):

family

Font family.

size

Font size in points. To give the size in pixels, use a negative value.

weight

Font thickness. Use one of NORMAL or BOLD. Default is NORMAL.

slant

Font slant. Use one of NORMAL or ITALIC. Default is NORMAL.

underline

Font underlining. If 1 (true), the font is underlined. Default is 0 (false).

overstrike

Font strikeout. If 1 (true), a line is drawn over text written with this font. Default is 0 (false).

System fonts

Tk also supports system specific font names. Under X, these are usually font aliases like fixed, 6x10, etc.

Under Windows, these include ansi, ansifixed, device, oemfixed, system, and systemfixed:

On the Macintosh, the system font names are application and system.

Note that the system fonts are full font names, not family names, and they cannot be combined with size or style attributes. For portability reasons, avoid using these names wherever possible.

X Font Descriptors

X Font Descriptors are strings having the following format (the asterisks represent fields that are usually not relevant. For details, see the Tk documentation, or an X manual):

--family-weight-slant--size----*-charset

The font family is typically something like Times, Helvetica, Courier or Symbol.

The weight is either Bold or Normal. Slant is either R for “roman” (normal), I for italic, or O for oblique (in practice, this is just another word for italic).

Size is the height of the font in decipoints (that is, points multiplied by 10). There are usually 72 points per inch, but some low-resolution displays may use larger “logical” points to make sure that small fonts are still legible. The character set, finally, is usually ISO8859-1 (ISO Latin 1), but may have other values for some fonts.

The following descriptor requests a 12-point boldface Times font, using the ISO Latin 1 character set:

--Times-Bold-R--120----*-ISO8859-1

If you don’t care about the character set, or use a font like Symbol which has a special character set, you can use a single asterisk as the last component:

--Symbol----80-

A typical X server supports at least Times, Helvetica, Courier, and a few more fonts, in sizes like 8, 10, 12, 14, 18, and 24 points, and in normal, bold, and italic (Times) or oblique (Helvetica, Courier) variants. Most servers also support freely scaleable fonts. You can use programs like xlsfonts and xfontsel to check which fonts you have access to on a given server.

This kind of font descriptors can also be used on Windows and Macintosh. Note that if you use Tk 4.2, you should keep in mind that the font family must be one supported by Windows (see above).

Text Formatting

While text labels and buttons usually contain a single line of text, Tkinter also supports multiple lines. To split the text across lines, simply insert newline characters (\n) where necessary.

By default, the lines are centered. You can change this by setting the justify option to LEFT or RIGHT. The default value is CENTER.

You can also use the wraplength option to set a maximum width, and let the widget wrap the text over multiple lines all by itself. Tkinter attempts to wrap on whitespace, but if the widget is too narrow, it may break individual words across lines.

Borders

All Tkinter widgets have a border (though it’s not visible by default for some widgets). The border consists of an optional 3D relief, and a focus highlight region.
Relief #

The relief settings control how to draw the widget border:

borderwidth (or bd)

This is the width of the border, in pixels. Most widgets have a default borderwidth of one or two pixels. There’s hardly any reason to make the border wider than that.

relief

This option controls how to draw the 3D border. It can be set to one of SUNKEN, RAISED, GROOVE, RIDGE, and FLAT.

Focus Highlights

The highlight settings control how to indicate that the widget (or one of its children) has keyboard focus. In most cases, the highlight region is a border outside the relief. The following options control how this extra border is drawn:

highlightcolor

This option is used to draw the highlight region when the widget has keyboard focus. It’s usually black, or some other distinct contrast color.

highlightbackground

This option is used to draw the highlight region when the widget doesn’t have focus. It’s usually same as the widget background.

highlightthickness

This option is the width of the highlight region, in pixels. It is usually one or two pixels for widgets that can take keyboard focus.

Cursors

cursor

This option controls which mouse cursor to use when the mouse is moved over the widget.

If this option isn’t set, the widget uses the same mouse pointer as its parent.

Note that some widgets, including the Text and Entry widgets, set this option by default.
处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道