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} {_type}>')
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()
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
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame