Ad

How To Render HTML In Python?

- 1 answer

This code does not work for me

I would like to know a way to render html using python without tkinterhtml

When loading google.com, I get the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python scripts\project\file.py", line 10, in search
    html = htmlBytes.decode("utf8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe7 in position 10955: invalid continuation byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\python scripts\project\file.py", line 12, in search
    html = htmlBytes.decode("utf16")
UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x3e in position 14624: truncated data

I am working on this for a project and am launching the code using the import keyword. My code doesn't work and closes the window after the search funtion finishes.

import urllib.request
import tkinter
from tkinterhtml import HtmlFrame

def search():
    url = urlInput.get()
    page = urllib.request.urlopen(url)
    htmlBytes = page.read()
    try:
        html = htmlBytes.decode("utf8")
    except:
        html = htmlBytes.decode("utf16")
    frame.set_content(html)

    page.close()

screen = tkinter.Tk()
screen.geometry("700x700")
frame = HtmlFrame(screen, horizontal_scrollbar="auto")
urlInput = tkinter.Entry(screen)
urlInput.grid(column=0,row=0,columnspan=10,rowspan=4,sticky="news")
searchBtn = tkinter.Button(screen,text="search",command=search)
searchBtn.grid(row=0,column=11,sticky="news")
screen.mainloop()
Ad

Answer

tkinter does not have the ability to render HTML. You'll have to use a third-party library. Since you explicitly said you don't want to use tkinterhtml you'll have to find some other third-party renderer.

Ad
source: stackoverflow.com
Ad