fonts in tkinter label code example

Example 1: how to change the font of a label in tkinter

#How to change the font of a label in Tkinter

#Import 
from tkinter import *

#Screen
window = Tk()
window.title("New Window")
window.geometry("300x250")

Label(window, text = "This is my new project in python!", font = ("Bahnschrift", 14)).pack()
#-------------------------------------------------------------------------------------------
#'font' tells python that we are going to do something with the font
#-------------------------------------------------------------------------------------------
#'()' are needed becuase that is just how python works
#-------------------------------------------------------------------------------------------
#'"___"' put your font name in the quotes
#-------------------------------------------------------------------------------------------
#10 put vyour fontsize after adding a comma after the font name
#-------------------------------------------------------------------------------------------

Example 2: add font to the label in window tkinter

import tkinter

window = tkinter.Tk()       # creating the window object
window.title('my first GUI program')
window.minsize(width=600, height=500)

# label 
# adding text to the middle of the window
my_label = tkinter.Label(text='I am a Label', font=('Cursive', 24, 'bold'))
my_label.pack()

window.mainloop()           # keeping the window until we close it

Example 3: tkinter label fontsize

pythonCopyimport tkinter as tk
import tkinter.font as tkFont
    
app = tk.Tk()

fontStyle = tkFont.Font(family="Lucida Grande", size=20)

labelExample = tk.Label(app, text="20", font=fontStyle)

def increase_label_font():
    fontsize = fontStyle['size']
    labelExample['text'] = fontsize+2
    fontStyle.configure(size=fontsize+2)

def decrease_label_font():
    fontsize = fontStyle['size']
    labelExample['text'] = fontsize-2
    fontStyle.configure(size=fontsize-2)
    
buttonExample1 = tk.Button(app, text="Increase", width=30,
                          command=increase_label_font)
buttonExample2 = tk.Button(app, text="Decrease", width=30,
                          command=decrease_label_font)

buttonExample1.pack(side=tk.LEFT)
buttonExample2.pack(side=tk.LEFT)
labelExample.pack(side=tk.RIGHT)
app.mainloop()

Example 4: how to create a label in tkinter

#How to create a label in Tkinter

from tkinter import *

#First create a screen
new_window = Tk()
new_window.title("My Python Project")
new_window.geometry("300x250")

Label(new_window, text = "Hello World").pack()
#Label : Tells Python that a label is being created
#new_window : whatever screen you want the label to be shown on ; name the screen whatever you like
#text : this tells that there is text being typed in the label
#.pack() : shows the label on the screen