Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up TkHtml (a Tk widget) with Python

I need a simple way to render HTML pages retrieved with Requests (python-requests.org). I'm using Python 3.2 on Windows.

I was using tkinter, and I found a Tk widget called TkHtml. It is described at http://tkhtml.tcl.tk/ and a DLL is downloadable from http://www.hwaci.com/sw/tkhtml/. I found a python wrapper at http://tix.sourceforge.net/Tixapps/src/Python/TkHtml.py.

I don't know how to get TkHtml working in python. Is there some standard way of handling 3rd party Tk widgets?

I put tkhtml.dll in Python32\DLLs (no idea if this is right) and put TkHtml.py in Python32\Lib\site-packages. I went ahead and fixed the imports in TkHtml.py to work with Python 3 (changed tkFileDialog to tkinter.filedialog and Tkinter to tkinter).

When I do:

import TkHtml
app=TkHtml.TestApp()

I get the error:

...
File "C:\Program Files\Python32\lib\site-packages\TkHtml.py", line 45, in __init__
master.tk.call("package", "require", "tkhtml")
_tkinter.TclError: can't find package tkhtml

Any ideas?

like image 432
A Coder Avatar asked Apr 14 '12 21:04

A Coder


People also ask

Is Tk () a widget?

Tk is a package that provides a rich set of graphical components for creating graphical applications with Tcl. Tk provides a range of widgets ranging from basic GUI widgets like buttons and menus to data display widgets. The widgets are very configurable as they have default configurations making them easy to use.

Can tkinter display HTML?

The tkhtmlview module is a collection of Tkinter widgets whose text can be set in HTML format. An HTML widget isn't a web browser frame, it's only a simple and lightweight HTML parser that formats the tags used by the Tkinter Text base class.

How do I run a tkinter in Python?

Tkinter ProgrammingImport the Tkinter module. Create the GUI application main window. Add one or more of the above-mentioned widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user.

Can I use HTML as Python GUI?

As you can see, you would be able to create your whole GUI using HTML, we just must add <script type="text/javascript" src="/eel. js"></script> , this script will allow us to call our exposed python functions. We have completed our graphical user interface, now, we must create our python file.


2 Answers

I created a pip-installable Python wrapper for Tkhtml3: https://bitbucket.org/aivarannamaa/tkinterhtml

It comes with TkHtml3 binaries for Windows, Mac and Linux.

like image 168
Aivar Avatar answered Oct 16 '22 09:10

Aivar


Find your Python tcl directory eg. C:\Python32\tcl . This is where tcl/tk extensions go.

Create a new folder in there called Tkhtml.

Into this folder put (1) your .dll file , (2) a text file called pkgIndex.tcl

pkgIndex.tcl contains a line like this:

package ifneeded Tkhtml 0.0 [list load [file join $dir tkhtml.dll]]

If you can do this at the python prompt,

>>> import Tkinter #tkinter
>>> root = Tkinter.Tk()
>>> root.tk.eval('package require Tkhtml')
'0.0'

... then the package is available. The string '0.0' represents the version number.

like image 34
noob oddy Avatar answered Oct 16 '22 07:10

noob oddy