How to use HTML in Tkinter - Python?
Last Updated :
31 Aug, 2021
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
# Import Module
from tkinter import *
from tkhtmlview import HTMLLabel
# Create Object
root = Tk()
# Set Geometry
root.geometry("400x400")
# Add label
my_label = HTMLLabel(root, html="""
<h1>GEEKSFORGEEKS</h1>
<h2>GEEKSFORGEEKS</h2>
<h3>GEEKSFORGEEKS</h3>
<h4>GEEKSFORGEEKS</h4>
<h5>GEEKSFORGEEKS</h5>
<h6>GEEKSFORGEEKS</h6>
""")
# Adjust label
my_label.pack(pady=20, padx=20)
# Execute Tkinter
root.mainloop()
Output:
Example 2: Use anchor, paragraph & image tags
Python3
# Import Module
from tkinter import *
from tkhtmlview import HTMLLabel
# Create Object
root = Tk()
# Set Geometry
root.geometry("400x400")
# Add label
my_label = HTMLLabel(root, html="""
<a href='https://www.geeksforgeeks.org/'>GEEKSFORGEEKS</a>
<p>Free Tutorials, Millions of Articles, Live, Online and Classroom Courses ,Frequent Coding Competitions ,Webinars by Industry Experts, Internship opportunities and Job Opportunities.</p>
<img src="gfg.png">
""")
# Adjust label
my_label.pack(pady=20, padx=20)
# Execute Tkinter
root.mainloop()
Output:
Â
Example 3: Use list tag and add a link to each tag
Python3
# Import Module
from tkinter import *
from tkhtmlview import HTMLLabel
# Create Object
root = Tk()
# Set Geometry
root.geometry("400x400")
# Add label
my_label = HTMLLabel(root, html="""
<ul>
<li><a href='https://www.geeksforgeeks.org/python-programming-language/'>Python</a></li>
<li><a href='https://www.geeksforgeeks.org/c-plus-plus/'>C++</a></li>
<li><a href='https://www.geeksforgeeks.org/java/'>Java</a></li>
</ul>
""")
# Adjust label
my_label.pack(pady=20, padx=20)
# Execute Tkinter
root.mainloop()
Output:
Similarly, we can use all other tags.
Similar Reads
How to Install Tkinter on Linux? Tkinter is the most popular Python library used to create GUI-based applications. The name Tkinter comes from the Tk interface. In this article, we will look into the process of installing Tkinter in a Linux system. Note: Tkinter module comes in bundled with Python, so this article can be helpful fo
2 min read
What is Tkinter for Python? Tkinter is a standard Python GUI (Graphical User Interface) library that provides a set of tools and widgets to create desktop applications with graphical interfaces. Tkinter is included with most Python installations, making it easily accessible for developers who want to build GUI applications wit
2 min read
How to show webcam in TkInter Window - Python Python offers various modules for creating GUI applications, out of which, Tkinter lets users do various tasks inside their app. Thus, while creating a GUI app, have you ever felt the need to let the user open the camera on a specific condition? Don't know, how to achieve that. Continue reading this
4 min read
How to Install Tkinter on MacOS? In this article, we will learn how to install Tkinter in Python on MacOS. Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's de-facto standard GUI. Installation:Method 1: Using pip to install Tkinter Follow the below steps to
2 min read
How to Use Entry Box on Canvas - Tkinter There are various ways of creating GUI applications in Python. One of the easiest and most commonly used ways is through the use of the Tkinter module. Tkinter offers various widgets for creating web apps. Â One such widget is the Entry box, which is used to display a single line of text. Are you fac
5 min read