Skip to content
Related Articles

Related Articles

Improve Article

How to use HTML in Tkinter – Python?

  • Difficulty Level : Easy
  • 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :