Tkinter教程之Font篇

来源:互联网 发布:淘宝一秒付款 编辑:程序博客网 时间:2024/06/04 23:20
'''Tkinter教程之Font篇'''
# Tkinter中其它常用的一些功能
'''1.字体使用'''
# -*- coding: cp936 -*-
#
 改变组件的显示字体
from Tkinter import *
root 
= Tk()
# 创建一个Label
for ft in ('Arial',('Courier New',),('Comic Sans MS',),'Fixdsys',('MS Sans Serif',),('MS Serif',),'Symbol','System',('Times New Roman',),'Verdana'):
    Label(root,text 
= 'hello sticky',font = ft ).grid()

root.mainloop()
# 在Windows上测试字体显示,注意字体中包含有空格的字体名称必须指定为tuple类型。
'''2.使用系统已有的字体'''
# -*- coding: cp936 -*-
#
 Font来创建字体
from Tkinter import *
# 引入字体模块
import tkFont
root 
= Tk()
# 创建一个Label
#
 指定字体名称、大小、样式
ft = tkFont.Font(family = 'Fixdsys',size = 20,weight = tkFont.BOLD)
Label(root,text 
= 'hello sticky',font = ft ).grid()

root.mainloop()
# 使用tkFont.Font来创建字体。
'''3.字体创建属性优先级'''
# -*- coding: cp936 -*-
#
 使用系统已有的字体显示
from Tkinter import *
import tkFont
root 
= Tk()
# 创建一个Label
#
 指定字体名称、大小、样式
#
 名称是系统可使用的字体
ft1 = tkFont.Font(family = 'Fixdsys',size = 20,weight = tkFont.BOLD)
Label(root,text 
= 'hello sticky',font = ft1 ).grid()

ft2 
= tkFont.Font(font = ('Fixdsys','10',tkFont.NORMAL),size = 40)
Label(root,text 
= 'hello sticky',font = ft2).grid()

root.mainloop()
# 创建字体有font等其它属性,
#
 如果font指定了,有几个参数将不再起作用,如:family,size,weight,slant,underline,overstrike
#
 例子中演示的结果是ft2中字体大小为10,而不是40
'''4.得到字体的属性值'''
# -*- coding: cp936 -*-
#
 测试measure和metrics属性
from Tkinter import *
import tkFont
root 
= Tk()
# 创建一个Label
ft1 = tkFont.Font(family = 'Fixdsys',size = 20,weight = tkFont.BOLD)
Label(root,text 
= 'hello font',font = ft1 ).grid()

ft2 
= tkFont.Font(font = ('Fixdsys','10',tkFont.NORMAL),size = 40)
Label(root,text 
= 'hello font',font = ft2).grid()

# 得到字体的宽度
print ft1.measure('hello font')
print ft2.measure('hello font')

# 打印两个字体的属性
for metric in ('ascent','descent','linespace','fixed'):
    
print ft1.metrics(metric)
    
print ft2.metrics(metric)
root.mainloop()
# 使用这两个方法得到已创建字体的相关属性值
'''5.使用系统指定的字体'''
# -*- coding: cp936 -*-
#
 使用系统字体:以下测试是Windows上的系统指定字体
from Tkinter import *
import tkFont
root 
= Tk()
for ft1 in ('ansi','ansifixed','device','oemfixed','system','systemfixed'):
    Label(root,text 
= 'hello font',font = ft1 ).grid()

root.mainloop()
# X Window上的系统指定字体:fixed,6x10等
'''6.使用X Font Descriptor'''
# -*- coding: cp936 -*-
#
 使用X Font Descriptor
from Tkinter import *
import tkFont
root 
= Tk()
for ft in ('Times','Helvetica','Courier','Symbol',):
    Label(root,text 
= 'hello font',font = ('-*-%s-*-*-*--*-240-*')%(ft)).grid()

root.mainloop()
# X Font Descriptor格式:-*-family-weight-slant-*--*-size-*-*-*-*-charset
#
 这个例子是在Windows下测试,没有在Linux测试。