How to use HTML in Tkinter – Python?
Prerequisite: Tkinter
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
In this article, we will learn, How we can use HTML tags in Tkinter. Here we will use the tkhtmlview Module.
The tkhtmlview module is a collection of Tkinter widgets whose text can be set in HTML format. An HTML widget isn’t a web browser frame, it’s only a simple and lightweight HTML parser that formats the tags used by the Tkinter Text base class.
Installation
pip install tkhtmlview
List of HTML Tags supports in tkhtmlview:
- a
- b
- br
- code
- div
- em
- h1, h2, h3, h4, h5, h6
- i
- img
- li, ul, ol
- mark
- p
- pre
- span
- strong
- u
We will use HTMLLabel() class for writing HTML tags
HTMLLabel(): Text-box widget with label appearance
Syntax:
HTMLLabel(Object Name, html="ENTER HTML CODE")
Below are some examples to depict how to use HTML in Tkinter GUI.
Example 1: Use heading tags.
Python3
from tkinter import *
from tkhtmlview import HTMLLabel
root = Tk()
root.geometry( "400x400" )
my_label = HTMLLabel(root, html =
)
my_label.pack(pady = 20 , padx = 20 )
root.mainloop()
|
Output:
Example 2: Use anchor, paragraph & image tags
Python3
from tkinter import *
from tkhtmlview import HTMLLabel
root = Tk()
root.geometry( "400x400" )
my_label = HTMLLabel(root, html =
)
my_label.pack(pady = 20 , padx = 20 )
root.mainloop()
|
Output:
Example 3: Use list tag and add a link to each tag
Python3
from tkinter import *
from tkhtmlview import HTMLLabel
root = Tk()
root.geometry( "400x400" )
my_label = HTMLLabel(root, html =
)
my_label.pack(pady = 20 , padx = 20 )
root.mainloop()
|
Output:
Similarly, we can use all other tags.
Last Updated :
31 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...