Once you have an HTML window created, the next challenge is to display the HTML text in the window. The following four methods are used to get HTML text into the window.
The most direct is the method SetPage(source), where the source parameter is a string containing the HTML source that you want displayed in the window.
Once you have text in the page, you can append HTML to the end of text that is currently in the window with the method AppendToPage(source). For both the SetPage() and AppendToPage() methods, the code assumes that the source is
HTML, meaning that if you pass it plain text, the spacing is ignored in keeping with the HTML standard.
If you want your window to behave more like a browser by actually browsing external resources, you have two options. The method LoadFile(filename) reads the contents of a local file and displays them in the window. In this case, the window takes advantage of the MIME type of the file to load an image file or an HTML file. If it can't decide which type the file is, it loads the file as plain text. If the document that is loaded contains relative links to images or other documents, the base location used to resolve those links is the location of the original file.
Of course, a real browser isn't limited to mere local files. You can load a remote URL with the method LoadPage(location), where the location is typically a URL, but could also be a pathname to a local file. The MIME type of the URL is used to determine how the page is loaded. Later in this chapter, we'll describe how to add support for new file types.
Figure 16.2 displays a page loaded into an HTML window.
Listing 16.2 displays the code used to display figure 16.2.
Listing 16.2 Loading the HTML window content from a web page import wx import wx.html class MyHtmlFrame(wx.Frame):
wx.Frame._init_(self, parent, -1, title, size=(600,400))
html = wx.html.HtmlWindow(self) if "gtk2" in wx.Platformlnfo: html.SetStandardFonts()
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Simple HTML Browser")
The key point in listing 16.2 is the LoadPage() method. A more full-featured window would probably display the URL in a text box, and change the window contents when the user enters a new URL.
Was this article helpful?