How to display rendered html content in text widget of tkinter in python 3.4.x
Asked Answered
N

4

14

I want to display my mails as it is in text widget of tkinter in python 3.4. It can be printed as HTML formatted way by using message.HTMLBody. How do I render it in my text widget and display the content

Nahuatlan answered 7/5, 2016 at 3:51 Comment(1)
Possible duplicate of python tkinter with a simple web wrapperGainor
V
13

I've managed to render simple html tags using tkhtml.

Install using pip3 install tkinterhtml, then, using the package example:

from tkinterhtml import HtmlFrame

frame = HtmlFrame(root, horizontal_scrollbar="auto")
 
frame.set_content("<html></html>")
 

If you want to directly render a webpage, you can do this to request and render it:

import urllib
frame.set_content(urllib.request.urlopen("https://duckduckgo.com").read().decode())

Hope it helps :)

Verbid answered 21/2, 2018 at 15:47 Comment(0)
I
5

Short of anything past very basic HTML tkinter just wasn't built for this. There's tkhtml as one option, but anything past basic html you'll want to look elsewhere. Tk can do a lot of amazing things, but embedding webpages / content isn't really one of them.

If you just want the text / some basic images / formatting from your mail, then you can scrape the data and then render and format it through the standard tk widgets.

I don't really like recommending other packages / options as it tends to be highly opinionated, but check out PyQt. Specifically, the QtWebkit.

Intendment answered 7/5, 2016 at 4:36 Comment(4)
can you help me out with an example code of tkhtml? ThanksNahuatlan
It sounds like tkinter would be capable of rendering Wiki page html, something on par with Wikipedia pages. Do you know about html forms?Pronate
If you mean display forms as in html forms then in a form yes... I've built plenty of GUI's that parse the html for forms and dynamically recreates them through tk widgets etc and then sends the data off for web scraping purposesIntendment
So javascript inside the html wouldnt be displayed?Grover
C
2

The package tkhtmlview is a collection of tkinter widgets whose text can be set in HTML format, that is just what I needed.

Example

import tkinter as tk
from tkhtmlview import HTMLLabel

root = tk.Tk()
html_label = HTMLLabel(root, html='<h1 style="color: red; text-align: center"> Hello World </H1>')
html_label.pack(fill="both", expand=True)
html_label.fit_height()
root.mainloop()
Cruciform answered 11/11, 2022 at 18:38 Comment(1)
any idea how to retrieve HTML from HTMLText if I made edits to the text when I run the script?Rickyrico
A
0

Here is how to use tkhtmlview:

from tkhtmlview import HTMLLabel

# Create text widget for HTML input
html_input = tk.Text(root, wrap="word", font=menu_font)
html_input.pack(fill="both", expand=True, padx=10, pady=10)
# Insert some default HTML
html_input.insert('1.0', HTML)

def copy_HTML():
    HTML_contents = html_input.get("1.0", "end-1c")
    copy_to_clipboard(HTML_contents)

def copy_to_clipboard(clip):
    root.clipboard_clear()
    root.clipboard_append(clip)
    root.update()

copy_button = ttk.Button(root, text="Copy", style="Accent.TButton", command=copy_HTML)
copy_button.pack(side="left", padx=5)

Snippets taken from my HTML Editor project

Anselm answered 3/7 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.