Tkinter Text weight参数

Tkinter Text weight参数

weight用于设置Text文字区域的字是否是粗体,下面将以实例说明此参数对于文字区域字形的影响。

示例1

先使用Frame建立一个Toolbar,然后将family对象放在此Toolbar内,同时靠左对齐。然后建立weight对象,默认的weight是normal,将此对象放在family对象右边,用户可以在Text文字区域输入文字,然后可以选择字形或是weight方式,可以看到所输入的文字将因所选择的字形或weight方式而有不同的变化。

from tkinter import * 
from tkinter.font import Font

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family
def weightChanged(event):             # weight family更新
    f=Font(weight=weightVar.get())    # 取得新的fong weight
    text.configure(font=f)            # 更新text的font weight

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)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

输出:

Tkinter Text weight参数

上述程序实例所使用的OptionMenu是使用tkinter的Widget,如果使用tkinter.ttk将看到不一样的外观

示例1

用tkinter.ttk模块的OptionMenu

from tkinter import * 
from tkinter.font import Font
from tkinter.ttk import * 

def familyChanged(event):             # font family更新
    f=Font(family=familyVar.get())    # 取得新的fong family
    text.configure(font=f)            # 更新text的font family
def weightChanged(event):             # weight family更新
    f=Font(weight=weightVar.get())    # 取得新的fong weight
    text.configure(font=f)            # 更新text的font weight

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)

# 建立Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()

root.mainloop()

输出:

Tkinter Text weight参数

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程