Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to use HTML in Tkinter – Python?

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




# 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.

RELATED ARTICLES

Most Popular

Recent Comments