HomeLinuxTkinter Fonts

Tkinter Fonts


Tkinter is a Python library used to construct customized GUI for purposes with the assistance of code. Every time you’re working with GUI, it is advisable to think about the UI and the UX, and fonts assist rather a lot in the case of growing consumer interplay with the appliance. There are three totally different strategies via which you’ll be able to simply change or configure the font on your tkinter utility, which can all be coated on this information.

This information will showcase the next strategies to configure Tkinter Fonts:

Technique 1: Utilizing the Textual content.configure() Technique

The tkinter.Textual content() is used to outline using Textual content in a Tkinter GUI in a variable, and the configure() technique is utilized to this Textual content variable to configure the properties of the “font.” This configure() technique takes in varied arguments, however we’re solely involved with setting the worth of “font,” which takes in a tuple of three values, household, dimension, and elegance.

To show using configure() technique, take the next code to create a Tkinter GUI:

import tkinter
body = tkinter.Tk()
body.title(“LinuxHint – Tkinter Fonts”)
body.geometry(“400×400”)
txtVar = tkinter.Textual content( body, top = 10)
txtVar.pack()
txtVar.configure(font = (“Ariel”, 15, “daring”))
body.mainloop()

 

On this code snippet:

  • First, import and create a Tkinter body by offering a title and dimension.
  • After that, create a brand new Textual content variable, “txtVar,” utilizing the Textual content() technique.
  • Apply the configure() technique on txtVar and cross the argument “font” with a price equal to a tuple dictating font as Ariel, dimension as “15”, and styling as “daring”

When this code is executed, it’ll produce the next Tkinter GUI in your machine:

The output confirms that you simply have been capable of configure the font for use inside your Tkinter GUI utilizing the configure() technique.

Technique 2: Utilizing tkinter.font.Font() With the configure() Technique

The Tkinter has a bundle named “font,” which accommodates a technique Font() used to create a Font variable. This Font variable can outline the font you wish to use inside your Tkinter GUI. Nevertheless, to make use of this, you’ll have to use the configure() technique and set the “font” arguments worth equal to the variable created via the Font() technique.

To show this, take the next code to create a Tkinter GUI:

import tkinter
from tkinter import font
body = tkinter.Tk()
body.title(“LinuxHint – Tkinter Fonts”)
body.geometry(“400×400”)
txtVar = tkinter.Textual content( body, top = 10)
txtVar.pack()
fontVar = tkinter.font.Font(household = “Arial”, dimension = 40 , weight = “regular”)
txtVar.configure(font = fontVar)
body.mainloop()

 

On this code snippet:

  • Import tkinter and font, and after that create a brand new tkinter GUI with title, form, and textual content.
  • Create a Font variable utilizing the Font() technique and cross it into the configure() technique’s “font” argument.

When this code is executed, it’ll produce the next Tkinter GUI in your machine:

The output reveals that you simply have been capable of configure the font of Tkinter GUI utilizing the configure() technique with the tkinter.font.Font() technique.

Technique 3: Set the Font of the Tkinter Body With option_add() Technique

One other technique that you should use to set the font of the Tkinter GUI is to make use of the option_add() technique and apply it on the body of Tkinter. Inside this option_add() technique, it is advisable to present two arguments:

  • First defines the component through which you’re including the choices
  • Second defines the choices that you’re deciding on for the component

To check out the working of the option_add() technique, you should use the next code snippet:

from tkinter import *
body= Tk()
body.geometry(“400×400”)
body.option_add( “*font”, “Arial 16 daring italic underline” )
Label(body, textual content = “Hi there Python Programmers”).pack()
body.mainloop()

 

Whenever you execute this code, you’ll get the next Tkinter window:

This output reveals that you simply have been capable of configure the font of the Tkinter window/body utilizing the option_add() technique.

Conclusion

The Tkinter window font can simply be configured through the use of the configure() technique and the option_add() technique. For the configure() technique, you’ll be able to cross in a tuple that defines the font that you simply wish to use, or you’ll be able to cross a font variable created via the Font() technique. To make use of the option_add() technique, merely apply it to the Tkinter’s body variable with the assistance of a dot operator, and throughout the arguments, outline the styling of the font that you simply wish to use.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments