免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2082 | 回复: 0
打印 上一主题 下一主题

Tkinter教程之Font篇 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-06 23:11 |只看该作者 |倒序浏览

               
               
                '''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测试。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/19742/showart_395741.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP