How can I display HTML in a wxPython window

HTML within wxPython is a useful mechanism for quickly describing a text layout involving styled text or a simple grid, as we discussed in chapter 6. The wxPython wx.html.HtmlWindow class is used for this purpose. Its goal is to display HTML, making it a fancy static text control with hypertext links. Figure 16.1 displays a modest example.

Listing 16.1 displays the code used to create figure 16.1.

Listing 16.1 Displaying the simple HtmlWindow import wx import wx.html class MyHtmlFrame(wx.Frame):

html = wx.html.HtmlWindow(self) if "gtk2" in wx.Platformlnfo: html.SetStandardFonts()


"Here is some <b>formatted</b> <i><u>text</u></i> " "loaded from a font color=\"red\">string</font>.")

Figure 16.1 A very simple


Figure 16.1 A very simple

HtmlWindow app = wx.PySimpleApp()

frm = MyHtmlFrame(None, "Simple HTML")



As you can see, the wx.html.HtmlWindow is declared and used the same way as every other wxPython widget, however, you must import the wx.html module, as wx. html.HtmlWindow is declared in a submodule of the wx package along with several HTML helper classes. The constructor is nearly identical to wx.ScrolledWindow.

wx.html.HtmlWindow(parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.html.HW_SCROLLBAR_AUTO, name="htmlWindow")

All of these parameters should look familiar by now. The most important difference is that the default style is wx.html.HW_SCROLLBAR_AUTO, which tells the HTML window to automatically add scrollbars as needed. The opposite style—never display scrollbars—uses the style flag wx.html.HW_SCROLLBAR_NEVER. One more HTML window style to use is wx.HW_NO_SELECTION, which prevents the user from making a text selection in the window.

When writing the HTML for display in the HTML window, remember to keep it simple. The widget is designed for simple styled text display, not for use as a full multimedia hypertext system. Most basic text tags are supported, but more advanced features like cascading style sheets and JavaScript are not. Highly complex tables and image setups may work, but you're setting yourself up for a fall. Table 16.1 contains the officially supported HTML tags. In general, tags and attributes behave as they would in a web browser, however, this is not a full-fledged browser, and there are likely to be cases that behave oddly. So that there is no confusion, table 16.1 is not in perfect HTML syntax—it's just the tag name followed by the list of supported attributes for that tag, if any. When using the HTML window you'll need to use proper HTML syntax.

Table 16.1 Valid HTML tags for the HTML window widget


Valid tags

Document Structure Tags

<a href name target> <body alignment bgcolor link text> <meta content http-equiv> <title>

Table 16.1 Valid HTML tags for the HTML window widget (continued)


Valid tags

<div align>

<hr align noshade size width> <p>

<b> <big> <blockquote> <center> <cite> <code> <em>

<font color face size>

<h1> <h2> <h3> <h4> <h5> <h6>

<samp> <small> <strike> <string>


List Tags

<dd> <dl> <dt> <li> <ol> <ul>

Image and Map Tags

<area coords href shape> <img align height src width usemap> <map name>

Table Tags

<table align bgcolor border cellpadding cellspacing valign width>

<td align bgcolor colspan rowspan valign width nowrap>

<th align bgcolor colspan valign width rowspan> <tr align bgcolor valign>

The HTML window uses wx.lmage to load and display images, so it can support all the image file formats that wx.lmage does.

Was this article helpful?

+1 -2


  • savanna
    How to import htmlwindow wxpython?
    5 years ago

Post a comment