One thing you've probably noticed in your web browser is that the display window is not the only element of the browser. Among the other elements of note are a title bar and status bar in the containing frame. Typically, the title bar displays the title of the HTML page being displayed, and the status bar displays information about links as they are moused over. In wxPython, there are a couple of shortcuts that allow you to set this relationship up quickly and easily. Figure 16.3 displays this relationship in action using a page from the wxWidgets online documentation. The title of the window display is based on the web page title, and the status bar text, also comes from the HTML window.
Listing 16.3 displays the code used to produce figure 16.3.
Listing 16.3 Loading the HTMLWindow content from a web page import wx import wx.html class MyHtmlFrame(wx.Frame):
def _init_(self, parent, wx.Frame._init_(self self.CreateStatusBar()
html = wx.html.HtmlWindow(self)
if "gtk2" in wx.Platformlnfo: Associating the html.SetStandardFonts() HTML to the frame html.SetRelatedFrame(self, self.GetTitle() + " -- %s")
html.SetRelatedStatusBar(0) <—i . . .. .. ... ^ ^ .
| Associating the HTML to the status bar html.LoadPage(
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Simple HTML Browser")
To set up the title bar relationship, use the method SetRelatedFrame(frame, format ). The frame argument is the wx.Frame where you want the HTML window's title information to display. The format argument is the string you want to display in the title bar of that frame. It can be any string you want, as long as it contains the pattern "%s" somewhere—that pattern is replaced by the <title> of the HTML page being displayed in the HTML window. A typical format argument would be something like "My wxPython Browser: %s". When a page is loaded in the window, the frame title is automatically replaced with the new page information.
To set up the status bar, use the method SetRelatedStatusBar(bar). This method must be called after SetRelatedFrame(), as it associates the HTML window with an element in the status bar of the related frame. The bar argument is the display slot in the status bar that should be used to display the status information. Typically, this will be 0, but it could be different if the frame has its status bar configured to display multiple slots. If the bar argument is -1, no messages are displayed. Once this relationship is created, when the mouse moves over an active anchor in the HTML display, the URL being linked to is displayed in the status bar.
Was this article helpful?