Tkinter 学习笔记 —— 标准属性
2017-07-01 15:42
309 查看
本笔记基于 Tkinter 8.5 reference: a GUI for Python
1、尺寸(Dimension)
表 3. 尺寸单位
2、坐标系统
基本单位是像素,左上的像素坐标是 (0, 0) 。
3、颜色
有 2 种表示方法:
16 进制数字描述
例如:
用 #fff 表示白色
用 #000000 表示黑色
用 #000fff000 表示纯绿
颜色名描述
例如:
white、black、red、green、 blue 、cyan、yellow 和 magenta 。
4、字体
导入 tkFont 模块,使用其 Font 类构造函数:
其中 options 包括:
获取一个 36-point 加粗 Helvetica 斜体:
获取你所在平台所有的字体系列:
注意:在调用该函数之前,先要创建根窗口。
所有 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 的字典。也可以传具体参数来获取特定值, 这些参数包括:
5、锚(Anchors)
6、浮雕风格(Relief styles)
7、位图
如下所示,从左到右依次是:error、 gray75、 gray50、 gray25、 gray12、 hourglass、 info、 questhead、 question、 以及 warning 。
可以使用自己的位图,只要是 .xbm 格式的就行。
8、光标
表 4. 光标选项的值
9、图像
使用 .xbm 格式位图显示 2 值图像。(BitmapImage)
使用 .gif .rpm .ppm 格式显示 full-color 图像。(PhotoImage)
使用 PIL 以支持更多格式图像。
BitmapImage 类
PhotoImage 类
10、几何字符串(Geometry strings)
几何字符串是描述桌面顶级窗口的大小和位置的标准方式。
一般形式:
其中:
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.
1、尺寸(Dimension)
表 3. 尺寸单位
Units | Description |
---|---|
c | 厘米 |
i | 英尺 |
m | 毫米 |
p | Printer’s points |
基本单位是像素,左上的像素坐标是 (0, 0) 。
3、颜色
有 2 种表示方法:
16 进制数字描述
Colors | description |
---|---|
#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 包括:
Items | Description |
---|---|
family | 设置字体系列 |
size | 字体尺寸,要获得 n 像素高字体,使用 -n |
weight | 字体粗细, bold 粗体, normal 常规 |
slant | italic 斜体,roman 不倾斜 |
underline | 1 下划线, 0 正常 |
overstrike | 1 启用, 0 不启用 |
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 的字典。也可以传具体参数来获取特定值, 这些参数包括:
Items | Description |
---|---|
ascent | 最高升部顶与基线之间高度的像素值 |
descent | 最低升部底与基线之间高度的像素值 |
fixed | 对于比例字体其值为 0 , 对于等宽字体其值为 1 |
linespace | 总高度的像素值 |
6、浮雕风格(Relief styles)
7、位图
如下所示,从左到右依次是:error、 gray75、 gray50、 gray25、 gray12、 hourglass、 info、 questhead、 question、 以及 warning 。
可以使用自己的位图,只要是 .xbm 格式的就行。
8、光标
表 4. 光标选项的值
Items | Description | Items | Description |
---|---|---|---|
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 |
使用 .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.
相关文章推荐
- 标准C++复习&学习笔记
- Hibernate3.2 学习笔记 映射的基本元素和属性
- ASP.NET控件开发学习笔记--第4回 为控件添加属性
- [学习笔记] EL 使用EL表达式获得作用域属性
- Struts学习笔记之在ActionForm中使用集合属性并自动组装(populate)数据
- IssueVision 学习笔记(二)-----为控件添加自定义属性和事件
- [Mono学习]树形视图TreeView学习笔记(三):对CellRenderer的属性进行定制
- C#2005 .NET3.0高级编程学习笔记————类和结构,类的数据成员,类的函数成员(方法、属性)
- Silverlight学习笔记:“依赖对象”和“依赖属性”
- CSS学习笔记Padding 属性中参数的定义与使用
- ruby学习笔记(1)--类的属性与方法
- AJAX 学习笔记(2) Ajax标准交互模式
- [J2EE学习笔记01]配置标准的J2EE开发环境
- 李浩学习计算机系列笔记——C#中属性
- CodeSmith学习笔记(一):设置属性为打开文件,保存文件
- 访问form对象的关键属性和方法【学习笔记】
- 学习标准――笔记
- AJAX 学习笔记(3) Ajax标准交互实例
- WPF学习笔记7: 附加属性(Attached Property)
- Python学习笔记五(面向对象-类-方法-属性)