Tkinter的简单字体选择器
tkfontchooser的Python项目详细描述
tkFontChooser
Tkinter的一个简单字体选择器,允许用户选择字体 他/她的系统中可用字体的系列。尺寸和样式 也可以设置文本的(粗体、斜体、下划线、删除线)。
此模块包含一个fontcooser类,该类实现字体 chooser和显示字体选择器和 当用户关闭字体选择器时返回所选字体。字体 作为字典返回,与函数返回的字典类似 tkfont.font.actual。
要求
- Linux、Windows、Mac
- python 2或3,带有tkinter+ttk(默认为Windows,但不适用于Linux)
安装
ubuntu:使用ppappa:j-4321-i/ppa
$ sudo add-apt-repository ppa:j-4321-i/ppa $ sudo apt-get update $ sudo apt-get install python(3)-tkfontchooser
archlinux:
the package is available on AUR
使用pip:
$ pip install tkfontchooser
示例
try:fromtkinterimportTkfromtkinter.ttkimportStyle,Button,LabelexceptImportError:fromTkinterimportTkfromttkimportStyle,Button,Labelfromsysimportplatformfromtkfontchooserimportaskfont# create main windowroot=Tk()style=Style(root)if"win"==platform[:3]:style.theme_use('vista')elif"darwin"inplatform:style.theme_use('clam')else:style.theme_use('clam')bg=style.lookup("TLabel","background")root.configure(bg=bg)label=Label(root,text='Chosen font: ')label.pack(padx=10,pady=(10,4))defcallback():# open the font chooser and get the font selected by the userfont=askfont(root)# font is "" if the user has cancellediffont:# spaces in the family name need to be escapedfont['family']=font['family'].replace(' ','\ ')font_str="%(family)s%(size)i%(weight)s%(slant)s"%fontiffont['underline']:font_str+=' underline'iffont['overstrike']:font_str+=' overstrike'label.configure(font=font_str,text='Chosen font: '+font_str.replace('\ ',' '))Button(root,text='Font Chooser',command=callback).pack(padx=10,pady=(4,10))root.mainloop()