Tkinter Text weight Parameter
weight is used to set whether the words in the Text text area are bold or not. The following example will illustrate the effect of this parameter on the text area glyphs.
Example#1
First use Frame to create a Toolbar, and then put the family object in this Toolbar, while aligned to the left. Then create the weight object, the default weight is normal, the object will be placed on the right side of the family object, the user can enter text in the Text text area, and then you can choose the font or weight way, you can see that the text entered will have different changes due to the selected font or weight way.
from tkinter import *
from tkinter.font import Font
def familyChanged(event): # font family changed
f=Font(family=familyVar.get()) # Get a new font family
text.configure(font=f) # Update font family of text
def weightChanged(event): # weight family changed
f=Font(weight=weightVar.get()) # Get new font weight
text.configure(font=f) # Update font weight of text
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create toolbar
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)
# create 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)
# create 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)
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()
root.mainloop()
Output:
The OptionMenu used in the above program example is a widget using tkinter, if you use tkinter.ttk you will see a different look.
Example#2
OptionMenu with tkinter.ttk module.
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def familyChanged(event): # font family changed
f=Font(family=familyVar.get()) # Get a new font family
text.configure(font=f) # Update font family of text
def weightChanged(event): # weight family changed
f=Font(weight=weightVar.get()) # Get new font weight
text.configure(font=f) # Update font weight of text
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create toolbar
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)
# create 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)
# create 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)
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.focus_set()
root.mainloop()
Output: