您的位置:首页 > 编程语言 > Python开发

Tkinter 学习笔记 —— 标准属性

2017-07-01 15:42 309 查看
本笔记基于 Tkinter 8.5 reference: a GUI for Python

1、尺寸(Dimension)

表 3. 尺寸单位

UnitsDescription
c厘米
i英尺
m毫米
pPrinter’s points
2、坐标系统



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

3、颜色

有 2 种表示方法:

16 进制数字描述

Colorsdescription
#rgb用 4 bit 描述每种颜色
#rrggbb用 8 bit 描述每种颜色
#rrrgggbbb用 12 bit 描述每种颜色
例如:

用 #fff 表示白色

用 #000000 表示黑色

用 #000fff000 表示纯绿

颜色名描述

例如:

white、black、red、green、 blue 、cyan、yellow 和 magenta 。

4、字体

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

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


其中 options 包括:

ItemsDescription
family设置字体系列
size字体尺寸,要获得 n 像素高字体,使用 -n
weight字体粗细, bold 粗体, normal 常规
slantitalic 斜体,roman 不倾斜
underline1 下划线, 0 正常
overstrike1 启用, 0 不启用
获取一个 36-point 加粗 Helvetica 斜体:

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


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

tkFont.families()


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

所有 Font 对象都有的方法:

.actual(option=None)

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

.cget(option)

返回给定 option 的值

.configure(option, …)

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

.copy()

返回一个 Font 对象的副本。

.measure(text)

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

.metrics(option)

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

ItemsDescription
ascent最高升部顶与基线之间高度的像素值
descent最低升部底与基线之间高度的像素值
fixed对于比例字体其值为 0 , 对于等宽字体其值为 1
linespace总高度的像素值
5、锚(Anchors)



6、浮雕风格(Relief styles)



7、位图

如下所示,从左到右依次是:error、 gray75、 gray50、 gray25、 gray12、 hourglass、 info、 questhead、 question、 以及 warning 。



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

8、光标

表 4. 光标选项的值

ItemsDescriptionItemsDescription

arrow
man

based_arrow_down
middlebutton

based_arrow_up
mouse

boat
penctil

bogosity
pirate

bottom_left_corner
plus

bottom_right_corner
question_arrow

bottom_side
right_ptr

bottom_tee
right_side

box_spiral
right_tee

center_ptr
rightbutton

circle
rtl_logo

clock
sailboat

coffee_mug
sb_down_arrow

cross
sb_h_double_arrow

cross_reverse
sb_letf_arrow

crosshair
sb_right_arrow

diamond_cross
sb_up_arrow

dot
sb_v_double_arrow

dotbox
shuttle

double_arrow
sizing

draft_large
spider

draft_small
spraycan

draped_box
star

exchange
target

fleur
tcross

gobbler
top_left_arrow

gumby
top_left_corner

hand1
top_right_corner

hand2
top_side

heart
top_tee

icon
trek

iron_cross
ul_angle

left_ptr
umbrella

left_side
ur_angle

left_tee
watch

leftbutton
xterm

ll_angle
X_cursor

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'


其中:

w 和 h 部分给出窗口宽度和高度(以像素为单位)。它们由字符 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 样式描述了两个线段以一定角度相遇的形状



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.





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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python tkinter gui