Tkinter Text size参数
size用于设置Text文字区域的字号,下面将以实例说明此参数对于文字区域字号的影响。
示例1
对Combobox对象设置字号,字号的区间是8~30,其中默认大小是12。将此对象放在weight对象右边,用户可以在Text文字区域输入文字,然后可以选择字形、weight或字号,可以看到所输入的文字将因所选择的字形或weight或字号而有不同的变化。
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def familyChanged(event): # font family更新
f=Font(family=familyVar.get()) # 取得新的font family
text.configure(font=f) # 更新text的font family
def weightChanged(event): # weight family更新
f=Font(weight=weightVar.get()) # 取得新的fontweight
text.configure(font=f) # 更新text的font weight
def sizeSelected(event): # size family更新
f=Font(size=sizeVar.get()) # 取得新font size
text.configure(font=f) # 取得text的font size
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# 建立工具栏
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)
# 建立font family OptionMenu
familyVar = StringVar()
familyFamily = ("Arial","Times","Courier")
familyVar.set(familyFamily[0])
family = OptionMenu(toolbar,familyVar,*familyFamily,command=familyChanged)
family.pack(side=LEFT,pady=2)
# 建立font weight OptionMenu
weightVar = StringVar()
weightFamily = ("normal","bold")
weightVar.set(weightFamily[0])
weight = OptionMenu(toolbar,weightVar,*weightFamily,command=weightChanged)
weight.pack(pady=3,side=LEFT)
# 建立font size Combobox
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,31)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack(side=LEFT)
# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()
root.mainloop()
输出: