Tkinter TkinterHtml 无法运行
Tkinter TkinterHtml not functioning
我正在尝试制作一个自动安装 pypi 包的应用程序,该应用程序解析了使用 urlopen
从网站中选择的用户。我设法获得 html,解码并将其提供给 TkinterHtml 小部件。但是,它会用 APPCRASH
使 python 解释器崩溃。所以我将检索到的 url 替换为更简单的 html text.However,这次它不会将图像渲染到小部件上(非图像标签可以像 p 一样正常工作)。我试图阅读 tcl 版本的文档,但无法将其应用于 python。
我从 here 安装了 tkinterhtml。
from urllib.request import urlopen
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
import ttk
from tkinterhtml import TkinterHtml
root = tk.Tk()
html = TkinterHtml(root, fontscale=0.8)
vsb = ttk.Scrollbar(root, orient=tk.VERTICAL, command=html.yview)
hsb = ttk.Scrollbar(root, orient=tk.HORIZONTAL, command=html.xview)
html.configure(yscrollcommand=vsb.set)
html.configure(xscrollcommand=hsb.set)
#html.tag("configure", "selection", "-background", "black")
html.grid(row=0, column=0, sticky=tk.NSEW)
vsb.grid(row=0, column=1, sticky=tk.NSEW)
hsb.grid(row=1, column=0, sticky=tk.NSEW)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
#data = urlopen("http://www.wikipedia.org").read().decode()
#html.parse(data)
html.parse("""
<html>
<body>
<h1>Hello world!</h1>
<p>First para</p>
<ul>
<li>first list item</li>
<li>second list item</li>
<img src="http://upload.wikimedia.org/wikipedia/en/3/38/Google_App_Engine_Logo.png"></img>
</ul>
</body>
</html>
""")
root.mainloop()
您需要一个函数来检索图像并创建 Tk 对象。这似乎对我有用,
from PIL import Image, ImageTk
import io
# needed to hold references
images = {}
def test(url):
fp = urlopen(url)
data = fp.read()
fp.close()
image = Image.open(io.BytesIO(data))
photo = ImageTk.PhotoImage(image)
images[url] = photo
return photo
root = tk.Tk()
# define imagecmd callback imagecmd=test in order to handle the images
html = TkinterHtml(root, fontscale=0.8, imagecmd=test)
tkhtml documentation(TkInterHtml 是 tkhtml3 的包装)指出
As well as for replacing entire document nodes (i.e. ), images
are used in several other contexts in CSS formatted documents, for
example as list markers or backgrounds. If the -imagecmd option is not
set to an empty string (the default), then each time an image URI is
encountered in the document, it is appended to the -imagecmd script
and the resulting list evaluated.
The command should return either an empty string, the name of a Tk
image, or a list of exactly two elements, the name of a Tk image and a
script. If the result is an empty string, then no image can be
displayed. If the result is a Tk image name, then the image is
displayed in the widget. When the image is no longer required, it is
deleted. If the result of the command is a list containing a Tk image
name and a script, then instead of deleting the image when it is no
longer required, the script is evaluated.
我正在尝试制作一个自动安装 pypi 包的应用程序,该应用程序解析了使用 urlopen
从网站中选择的用户。我设法获得 html,解码并将其提供给 TkinterHtml 小部件。但是,它会用 APPCRASH
使 python 解释器崩溃。所以我将检索到的 url 替换为更简单的 html text.However,这次它不会将图像渲染到小部件上(非图像标签可以像 p 一样正常工作)。我试图阅读 tcl 版本的文档,但无法将其应用于 python。
我从 here 安装了 tkinterhtml。
from urllib.request import urlopen
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
import ttk
from tkinterhtml import TkinterHtml
root = tk.Tk()
html = TkinterHtml(root, fontscale=0.8)
vsb = ttk.Scrollbar(root, orient=tk.VERTICAL, command=html.yview)
hsb = ttk.Scrollbar(root, orient=tk.HORIZONTAL, command=html.xview)
html.configure(yscrollcommand=vsb.set)
html.configure(xscrollcommand=hsb.set)
#html.tag("configure", "selection", "-background", "black")
html.grid(row=0, column=0, sticky=tk.NSEW)
vsb.grid(row=0, column=1, sticky=tk.NSEW)
hsb.grid(row=1, column=0, sticky=tk.NSEW)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
#data = urlopen("http://www.wikipedia.org").read().decode()
#html.parse(data)
html.parse("""
<html>
<body>
<h1>Hello world!</h1>
<p>First para</p>
<ul>
<li>first list item</li>
<li>second list item</li>
<img src="http://upload.wikimedia.org/wikipedia/en/3/38/Google_App_Engine_Logo.png"></img>
</ul>
</body>
</html>
""")
root.mainloop()
您需要一个函数来检索图像并创建 Tk 对象。这似乎对我有用,
from PIL import Image, ImageTk
import io
# needed to hold references
images = {}
def test(url):
fp = urlopen(url)
data = fp.read()
fp.close()
image = Image.open(io.BytesIO(data))
photo = ImageTk.PhotoImage(image)
images[url] = photo
return photo
root = tk.Tk()
# define imagecmd callback imagecmd=test in order to handle the images
html = TkinterHtml(root, fontscale=0.8, imagecmd=test)
tkhtml documentation(TkInterHtml 是 tkhtml3 的包装)指出
As well as for replacing entire document nodes (i.e. ), images are used in several other contexts in CSS formatted documents, for example as list markers or backgrounds. If the -imagecmd option is not set to an empty string (the default), then each time an image URI is encountered in the document, it is appended to the -imagecmd script and the resulting list evaluated.
The command should return either an empty string, the name of a Tk image, or a list of exactly two elements, the name of a Tk image and a script. If the result is an empty string, then no image can be displayed. If the result is a Tk image name, then the image is displayed in the widget. When the image is no longer required, it is deleted. If the result of the command is a list containing a Tk image name and a script, then instead of deleting the image when it is no longer required, the script is evaluated.