my_font_family(f_type) | Receives font family name and set it |
my_font_size(dir) | Receives direction and accordingly increase or decrease font size |
my_font_style() | Reads the radio button selected value and set the style accordingly. |
font1=['Times',12,'normal'] # default font details
menu_sub_family.add_command(label='Times',command=lambda:my_font_family('Times'))
menu_sub_family.add_command(label='Arial Black',command=lambda:my_font_family('Arial Black'))
Inside the function my_font_family() we receive the font family name and update the first element of the font1 ( list ) with this name. After updating, we used config to change the font option of the text widget.
def my_font_family(f_type): # select font family
font1[0]=f_type # set the font family
t1.config(font=font1) # config the font
menu_font.add_command(label="Zoom ++",command=lambda:my_font_size('increase')) #
menu_font.add_command(label="Zoom --",command=lambda:my_font_size('decrease'))
Inside the function my_font_size() we receive the direction as per the user click of the menu option. Here font size is 2nd element of the list font1. We are increasing or decreasing based on the parameter ( dir ) value.
def my_font_size(dir): # font size is increased or decreased
if(dir=='increase'):
font1[1]=font1[1]+2 # increase font size
elif(dir=='decrease'):
font1[1]=font1[1]-2 # decrease font size
t1.config(font=font1) # configure the font
r1_v = tk.IntVar(my_w) # for radio buttons
menu_sub_style.add_radiobutton(label='Normal',value=1,variable=r1_v,command=lambda:my_font_style())
menu_sub_style.add_radiobutton(label='Bold',value=2,variable=r1_v,command=lambda:my_font_style())
menu_sub_style.add_radiobutton(label='Underline',value=3,variable=r1_v,command=lambda:my_font_style())
Here user can select one of the three available options and on click of the radio buttons the function my_font_style() is executed. Inside this function we read the value of the variable r1_v and accourdingly the style for the font list is updated. Note that here the 3rd element of the list font1 is the style for the font list.
def my_font_style(): # selection of sub menu for style
if(r1_v.get()==1): # reading the value of r1_v
font1[2]='normal'
elif(r1_v.get()==2):
font1[2]='bold'
elif(r1_v.get()==3):
font1[2]='underline'
t1.config(font=font1) # configure the font
You can read more about the menu and its sub-menu here. import tkinter as tk
from tkinter import *
from tkinter import BOTH, END, LEFT
my_w = tk.Tk()
my_w.geometry("700x450")
my_frame = Frame(my_w, width=600, height=400)
my_frame.pack(fill="both", expand=1)
my_frame.pack_propagate(0)
l1 = tk.Label(my_frame,text='Your text', width=10) #add Label
l1.pack(fill="both", expand=0,padx=10)
#l1.grid(row=0,column=0,sticky='W')
font1=['Times',12,'normal'] # default font details
t1 = tk.Text(my_frame,height=4,font=font1) #text box
t1.pack(fill="both", expand=1,padx=10,pady=40)
#t1.grid(row=1,column=0,columnspan=2,padx=5,pady=10)
my_str="Welcome to plus2net \n Learn "
my_str = my_str + " about Tkitner Python baiscs and more..."
t1.insert(tk.END, my_str) # add default string to text widget
def my_font_family(f_type): # select font family
font1[0]=f_type # set the font family
t1.config(font=font1) # config the font
def my_font_size(dir): # font size is increased or decreased
if(dir=='increase'):
font1[1]=font1[1]+2 # increase font size
elif(dir=='decrease'):
font1[1]=font1[1]-2 # decrease font size
t1.config(font=font1) # configure the font
def my_font_style(): # selection of sub menu for style
if(r1_v.get()==1): # reading the value of r1_v
font1[2]='normal'
elif(r1_v.get()==2):
font1[2]='bold'
elif(r1_v.get()==3):
font1[2]='underline'
t1.config(font=font1) # configure the font
r1_v = tk.IntVar(my_w) # for radio buttons
menubar = tk.Menu(my_w)
menu_font = tk.Menu(menubar,title='my title' ,
tearoff=1,fg='red',bg='yellow') # file
menu_file=tk.Menu(menubar,tearoff=0) # edit menu
menu_sub_family=tk.Menu(menu_font,tearoff=0,bg='lightgreen')
menu_sub_style=tk.Menu(menu_font,tearoff=0,bg='lightblue')
menu_font.add_command(label="Zoom ++",command=lambda:my_font_size('increase')) #
menu_font.add_command(label="Zoom --",command=lambda:my_font_size('decrease')) #
menu_font.add_cascade(label='Font Family',menu=menu_sub_family ) # add sub
menu_font.add_cascade(label='Font style',menu=menu_sub_style ) # add sub
menu_sub_family.add_command(label='Times',command=lambda:my_font_family('Times'))
menu_sub_family.add_command(label='Arial Black',command=lambda:my_font_family('Arial Black'))
menu_sub_style.add_radiobutton(label='Normal',value=1,variable=r1_v,command=lambda:my_font_style())
menu_sub_style.add_radiobutton(label='Bold',value=2,variable=r1_v,command=lambda:my_font_style())
menu_sub_style.add_radiobutton(label='Underline',value=3,variable=r1_v,command=lambda:my_font_style())
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Font", menu=menu_font) # Top Line
menu_file.add_command(label="New") # Item 1 of file
menu_file.add_command(label="Open..") # Item 2
menu_file.add_separator()
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
Tkinter Text Python Tkinter Entry How to Validate user entered data
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.