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()
CodePudding user response:
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.
CodePudding user response:
Flask has a tool called render template, I use it for my website and it's not hard to implement either.... here is an example:
from flask import Flask
from flask import render_template
app = Flask(__name__)
#sets app route and renders file
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port="any port number here", debug=True)
#debug just refreshes the main file (app.py or something.py) when a change is made
I suggest you also check out the docs on Flask to get a full understanding of it, I really hope this answers your question https://flask.palletsprojects.com/en/2.0.x/