Search code examples
pythontextboxtkinterblockfont-size

How to increase the font size of a Text widget?


How to increase the font size of a Text widget?


Solution

  • There are several ways to specify a font: the simplest is a tuple of the form (family, size, style).

    import tkinter as tk
    
    root=tk.Tk()
    text=tk.Text(width = 40, height=4, font=("Helvetica", 32))
    text.pack()    
    root.mainloop()
    

    An arguably better solution is to create a font object, since the font will exist as an object and can be modified at runtime.

    import tkinter as tk
    from tkinter.font import Font
    
    root=tk.Tk()
    text_font = Font(family="Helvetica", size=32)
    text=tk.Text(width = 40, height=4, font=text_font)
    text.pack()
    root.mainloop()
    

    By using the Font class, you can change the font at any time, and every widget that uses the font will automatically be updated.

    For example, to change the size of the font for several widgets, you can change it once in the font and all widgets that use the font will see the change:

    text_font = Font(family="Helvetica", size=32)
    text1 = tk.Text(..., font=text_font)
    text2 = tk.Text(..., font=text_font)
    label = tk.Label(..., font=text_font)
    ...
    text_font.configure(weight="bold", size=24)