python - 如何在 python 中呈现 HTML?

标签 python html tkinter

此代码对我不起作用

我想知道一种使用 python 呈现 html 的方法没有 tkinterhtml

加载 google.com 时,出现错误:

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

我正在为一个项目工作,并使用 import 关键字启动代码。我的代码不起作用,并在搜索功能完成后关闭窗口。

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()

最佳答案

tkinter 没有渲染 HTML 的能力。您必须使用第三方库。由于您明确表示不想使用 tkinterhtml,因此您必须找到其他一些第三方渲染器。

关于python - 如何在 python 中呈现 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71564676/

相关文章:

python - 删除由多个 tkinter 小部件组成的对象

python - Tk TreeView 列排序

python - SQLAlchemy 中的用户定义函数创建

python - f2py 中的 malloc 错误

python - 合并排序数组算法

javascript - 我如何判断哪个组件当前处于事件状态 Angular 5

javascript - 我想使用 ajax 或任何其他方法将图像发送到数据库

python - 下载html django模板中的本地文件链接

javascript - 裁剪以 html 上传的图像并为其添加水印

python - 如何使用 Tkinter 包几何管理器重叠小部件?