Html tags parser for tkinter with tk_html_widgets (Python)

With this module tk_html_widgets you can easily get some html into a label in tkinter and have some html like kinda of rendering into tkinter GUI, that is very interesting to me. Follow me in discovering this module in this first chapter.

This is an example

import tkinter as tk
from tk_html_widgets import HTMLLabel

root = tk.Tk()

def tag(_type="h1", text="hello", color="black",text_align="center"):
	html_label = HTMLLabel(root, html=f'<{_type} style="color: {color}; text-align: {text_align}"> {text} ')
	html_label.pack(fill="both", expand=True)
	html_label.fit_height()
	return html_label

tag("h1", "This is H1", "blue")
tag("h2", "This is H2", "orange")
tag("p", "Welcome, this is a basic usage of the tk_html_widgets.", "red", "left")
tag("p",
	"We will make another video about this, because it is interesting and \
	we want to get a little deeper into it.",
	"darkgreen",
	"left")

root.mainloop()
The rendering output of the script

A simple way to do it

Honestly, there is a simple way to do it: with this function html it will be easier to put just the code in a multiline string and render the html code into the label.

import tkinter as tk
from tk_html_widgets import HTMLLabel

root = tk.Tk()

def tag(_type="h1", text="hello", color="black",text_align="center"):
	"Parses some html tags"
	html_label = HTMLLabel(root, html=f'<{_type} style="color: {color}; text-align: {text_align}"> {text} >/{_type}>')
	html_label.pack(fill="both", expand=True)
	html_label.fit_height()
	return html_label

def html(code):
	html_code = HTMLLabel(root,
		html=code)
	html_code.pack(fill="both", expand=True)
	html_code.fit_height()
	return html_code


html("""

Title

Hello this is the whole <u>page

  • 1
  • 2
""") root.mainloop()

Video

video tutorial for tk:html_widgets

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.