Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter TkinterHtml not functioning

I am trying to make a application which automatically installs pypi packages parsed the user selected from websites using urlopen. I managed to get the html, decoded and fed it to the TkinterHtml widget. However, it will crash down the python interpreter with an APPCRASH. So I replaced the retrieved url to a simpler html text.However, this time it wont render the image onto the widget(non image tags work fine like p). I have tried to read the documentation for the tcl version, but I was unable to apply it to python.

I installed tkinterhtml from here.

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()
like image 795
user3167683 Avatar asked Jul 22 '26 14:07

user3167683


1 Answers

You need a function that retrieves the image and creates a Tk object. This seemed to work for me,

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 is a wrapper for tkhtml3) states that

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.

like image 91
J.J. Hakala Avatar answered Jul 24 '26 05:07

J.J. Hakala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!