【问题标题】:Read a html file and display it in tkinter window读取 html 文件并将其显示在 tkinter 窗口中
【发布时间】:2020-05-31 10:20:46
【问题描述】:

我是 Python 新手,我正在做一个项目。当我在 GUI 上工作时,我遇到了一些困难。

我需要读取一个 html 文件并将其放在 tkinter 窗口中。我一直在寻找一些解决方案,有人推荐了 tkinterhtml。我复制了他的示例代码,但结果有问题。我不知道如何解决这个问题。如果有人可以帮助我,我将不胜感激。

from urllib.request import urlopen
from tkinter import *
import tkinterhtml as th

a = urlopen("https://www.baidu.com/?tn=78040160_14_pg&ch=8")
r = a.read()
d = r.decode()

root = Tk()
html = th.HtmlFrame(root)
html.pack()
html.set_content(d)
mainloop()

结果是:

Process finished with exit code -1073741819 (0xC0000005)

【问题讨论】:

    标签: python html user-interface tkinter


    【解决方案1】:

    这似乎是您的 python 安装的问题。尝试重新安装 python,因为它对我来说很好:

    另外,假设您要渲染 HTML,这在屏幕截图中不起作用,我不推荐 tkinterhtml - 它不能很好地工作。如果您不介意使用 PyQt,PyQt 会更好地满足您的需求。使用 pip 安装 PyQt5 和 PyQtWebEngine:

    pip install PyQt5
    pip install PyQtWebEngine
    

    然后根据您的需要尝试此示例代码:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWebEngineWidgets import *
    from PyQt5.QtWidgets import QApplication
    
    app = QApplication(sys.argv)
    
    web = QWebEngineView()
    web.load(QUrl("https://www.baidu.com/?tn=78040160_14_pg&ch=8"))
    web.show()
    
    sys.exit(app.exec_())
    

    这给了你这个:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2019-06-30
      • 2013-04-05
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多