Tkinter 学习笔记 —— 标准属性

来源:互联网 发布:手机全屏时钟软件 编辑:程序博客网 时间:2024/06/15 02:54

本笔记基于 Tkinter 8.5 reference: a GUI for Python

1、尺寸(Dimension)

表 3. 尺寸单位

Units Description c 厘米 i 英尺 m 毫米 p Printer’s points

2、坐标系统

coords.png

基本单位是像素,左上的像素坐标是 (0, 0)

3、颜色

有 2 种表示方法:

  • 16 进制数字描述
Colors description #rgb 用 4 bit 描述每种颜色 #rrggbb 用 8 bit 描述每种颜色 #rrrgggbbb 用 12 bit 描述每种颜色

例如:
#fff 表示白色
#000000 表示黑色
#000fff000 表示纯绿

  • 颜色名描述

例如:
whiteblackredgreenbluecyanyellowmagenta

4、字体

导入 tkFont 模块,使用其 Font 类构造函数:

import tkFontfont = tkFont.Font(option, ...)

其中 options 包括:

Items Description family 设置字体系列 size 字体尺寸,要获得 n 像素高字体,使用 -n weight 字体粗细, bold 粗体, normal 常规 slant italic 斜体,roman 不倾斜 underline 1 下划线, 0 正常 overstrike 1 启用, 0 不启用

获取一个 36-point 加粗 Helvetica 斜体:

helv36 = tkFont.Font(family='Helvetica',        size=36, weight='bold')

获取你所在平台所有的字体系列:

  tkFont.families()

注意:在调用该函数之前,先要创建根窗口。

所有 Font 对象都有的方法:

.actual(option=None)

如果不传递任何参数,你将获得关于字体实际属性的字典,这可能与你所请求的字体不同。要获取属性的值,请将其名称作为参数传递。

.cget(option)

返回给定 option 的值

.configure(option, …)

使用此方法更改字体上的一个或多个选项。例如,如果你有一个名为 titleFontFont 对象,调用 titleFont.configure(family=’times’, size=18),会将该字体将更改为 18pt 的 Times 字体。而且,使用该字体的任何部件也将随之更改。

.copy()

返回一个 Font 对象的副本。

.measure(text)

返回字符串将采用的宽度的像素值。

.metrics(option)

如果没有参数,则会返回包含所有 font metrics 的字典。也可以传具体参数来获取特定值, 这些参数包括:

Items Description ascent 最高升部顶与基线之间高度的像素值 descent 最低升部底与基线之间高度的像素值 fixed 对于比例字体其值为 0 , 对于等宽字体其值为 1 linespace 总高度的像素值

5、锚(Anchors)

anchors.png

6、浮雕风格(Relief styles)

relief.png

7、位图

如下所示,从左到右依次是:errorgray75gray50gray25gray12hourglassinfoquestheadquestion、 以及 warning

stdbitmaps.png

可以使用自己的位图,只要是 .xbm 格式的就行。

8、光标

表 4. 光标选项的值

Items Description Items Description cursor2.png arrow cursor41.png man cursor3.png based_arrow_down cursor42.png middlebutton cursor4.png based_arrow_up cursor43.png mouse cursor5.png boat cursor44.png penctil cursor6.png bogosity cursor45.png pirate cursor7.png bottom_left_corner cursor46.png plus cursor8.png bottom_right_corner cursor47.png question_arrow cursor9.png bottom_side cursor48.png right_ptr cursor10.png bottom_tee cursor49.png right_side cursor11.png box_spiral cursor50.png right_tee cursor12.png center_ptr cursor51.png rightbutton cursor13.png circle cursor52.png rtl_logo cursor14.png clock cursor53.png sailboat cursor15.png coffee_mug cursor54.png sb_down_arrow cursor16.png cross cursor55.png sb_h_double_arrow cursor17.png cross_reverse cursor56.png sb_letf_arrow cursor18.png crosshair cursor57.png sb_right_arrow cursor19.png diamond_cross cursor58.png sb_up_arrow cursor20.png dot cursor59.png sb_v_double_arrow cursor21.png dotbox cursor60.png shuttle cursor22.png double_arrow cursor61.png sizing cursor23.png draft_large cursor62.png spider cursor24.png draft_small cursor63.png spraycan cursor25.png draped_box cursor64.png star cursor26.png exchange cursor65.png target cursor27.png fleur cursor66.png tcross cursor28.png gobbler cursor67.png top_left_arrow cursor29.png gumby cursor68.png top_left_corner cursor30.png hand1 cursor69.png top_right_corner cursor31.png hand2 cursor70.png top_side cursor32.png heart cursor71.png top_tee cursor33.png icon cursor72.png trek cursor34.png iron_cross cursor73.png ul_angle cursor35.png left_ptr cursor74.png umbrella cursor36.png left_side cursor75.png ur_angle cursor37.png left_tee cursor76.png watch cursor38.png leftbutton cursor77.png xterm cursor39.png ll_angle cursor1.png X_cursor cursor40.png lr_angle

9、图像

  • 使用 .xbm 格式位图显示 2 值图像。(BitmapImage)
  • 使用 .gif .rpm .ppm 格式显示 full-color 图像。(PhotoImage)
  • 使用 PIL 以支持更多格式图像。

BitmapImage 类

tk.BitmapImage(file=f[, background=b][, foreground=c])
logo = tk.BitmapImage('logo.xbm', foreground='red')    Label(image=logo).grid()

PhotoImage 类

tk.PhotoImage(file=f)

10、几何字符串(Geometry strings)

几何字符串是描述桌面顶级窗口的大小和位置的标准方式。

一般形式:

'wxh±x±y'

其中:

  • wh 部分给出窗口宽度和高度(以像素为单位)。它们由字符 x 分隔。
  • 如果下一部分的形式为 +x ,则表示窗口左侧应距桌面左侧 x 像素。如果它为 -x,则窗口右侧距桌面右侧 x 像素。
  • 如果下一部分的形式为 +y ,则表示窗口顶部应距桌面顶部 y 像素。如果它为 -y,则窗口底部距桌面底部 y 像素。

11、窗口名

Tkinter 使用分层窗口路径名来命名所有的窗口。

  • 根窗口的名称是 ‘.’ 。
  • 子窗口具有 ‘.n’ 形式的名称,其中 ‘n’ 是字符串形式的一些整数。例如,名为 ‘.135932060’ 的窗口是根窗口(’.’)的子窗口。
  • 子窗口中的子窗口的名称格式为 ‘p.n’ ,其中 ‘p’ 是父窗口的名称,’n’ 是整数。例如,名为 ‘.135932060.137304468’ 的窗口父窗口为 ‘.135932060’ ,因此它是根窗口的孙子(grandchild)窗口。
  • 相对名称是最后一个 ‘.’ 之后的部分,因此,之前的例子中,孙子窗口的相对名称为 ‘137304468’ 。
  • 要获取部件 w 的路径名,使用 str(w)

12、Cap and join styles

  • 线的 cap 样式是线的末端的形状
  • join 样式描述了两个线段以一定角度相遇的形状

cap-join.png

13、dash 图案

  • dash

该选项为一个整数元组。第一个整数指定应绘制多少个像素。第二个整数指定在开始绘制之前应该跳过多少个像素,依此类推。当元组中的所有整数耗尽时,它们以相同的顺序重复使用,直到边框完成。
例如, dash=(3,5) 产生由 5 像素分隔的 3 像素虚线。dash=(7,1,1,1) 产生点划线图案。dash=(5,) 交替产生五像素线和五像素间隙。

  • dashoff

dashoff=n ,n 表示不循环的像素。

14、Matching stipple patterns

This may seem like an incredibly picky style point, but if you draw a graphic that has two objects with stippled patterns, a real professional will make sure that the patterns align along their boundary.

Here is an example. The left-hand screen shot shows two adjacent 100×100 squares stippled with the “gray12” pattern, but the right-hand square is offset vertically by one pixel. The short black line in the center of the figure is drawn along the boundary of the two figures.

stipulate1.pngstipulate0.png

The second screen shot is the same, except that the two 100×100 squares have their stipple patterns lined up.

In practice, this arises in two situations. The alignment of large stippled areas is controlled by an option named offset. For figures with stippled outlines, the outlineoffset option controls their alignment. Both options have values of one of these forms:

  • ‘x,y’: Offset the stipple patterns by this x and y value relative to the top-level window or to the canvas’s origin.

  • ‘#x,y’: For objects on a canvas, use offset x and y relative to the top-level window.

  • tk.NE, tk.SE, tk.SW, tk.NW: Align a corner of the stipple pattern with the corresponding corner of the containing object. For example, tk.NE means that the top left corner of the stipple pattern coincides with the top left corner of the area to be stippled.

  • tk.N, tk.E, tk.S, tk.W: Align the stipple pattern with the center of one side of the containing object. For example, tk.E means the center of the stipple pattern will coincide with the center of the right side of the area to be stippled.

  • tk.CENTER: Align the center of the stipple pattern with the center of the containing object.

原创粉丝点击