This article mainly introduces how to use wxPython in Python to implement basic browser functions. This article is from the IBM official website developer documentation. if you need it, you can refer to the following documents, most applications solve most browser problems by keeping HTML simple-or writing based on the lowest common features. However, even so, there are still issues with the font and layout. when new browsers are released and existing browsers are upgraded, it is no longer necessary to test the application. Alternative-only one browser is supported-typically not a user-friendly solution.
The obvious solution is to embed your own HTML window component in the application. Of course, writing such a window component from the very beginning requires a lot of work. Therefore, it seems reasonable to turn to a pre-encapsulated solution.
The business community has many choices and several open source software packages. This article will show you how to use Python as the binding language (C ++, Perl, and other languages are also supported) to use the wxHtml window component that is distributed as part of the wxWindows software package.
Although developers familiar with Python should be able to start from scratch without any wxPython experience, this article assumes that you have basic wxPython knowledge. In this article, we will create an independent browser application, while maintaining an architecture that is simple enough to migrate browser functions to existing applications is a simple task.
The most basic browser in the world
The first step is to assemble the minimum code required for applications that support wxHtml window components. The following code uses the wxHtml window component as the basic wxPython application in the main window.
Listing 1. basic sample browser code
from wxPython.wx import *from wxPython.html import *import os,sysclass exHtmlWindow(wxHtmlWindow): def __init__(self, parent, id, frame): wxHtmlWindow.__init__(self,parent,id)class exHtmlPanel(wxPanel): def __init__(self, parent, id, frame): wxPanel.__init__(self,parent,-1) self.html = exHtmlWindow(self, -1, frame) self.box = wxBoxSizer(wxVERTICAL) self.box.Add(self.html, 1, wxGROW) self.SetSizer(self.box) self.SetAutoLayout(true)class exFrame (wxFrame): def __init__(self, parent, ID, title): wxFrame.__init__(self,parent,ID,title,wxDefaultPosition,wxSize(600,750)) panel = exHtmlPanel(self, -1, self)class exApp(wxApp): def OnInit(self): frame = exFrame(NULL, -1, "Example Browser") frame.Show(true) self.SetTopWindow(frame) return trueapp = exApp(0)app.MainLoop()
If you have correctly installed wxPython, running the above code in the Python interpreter will generate a large window with an empty white panel (wxHtml window component. If any syntax error occurs, check the space issue, especially if you cut and paste the code into the interpreter or editor. If the Python interpreter shows that wxPython cannot be imported, check the installation to ensure that the installation is correct.
Of course, when you start the browser, what immediately appears is: we lack something... for example, the page loading mechanism. For some applications, this basic setting may be enough-if you already know what you want to deliver, you do not need to select your own page. A simple change is to pass additional parameters to the exHtmlPanel, that is, the page you want to visit:
Listing 2. modify the exHtmlPanel to load the page
class exHtmlPanel(wxPanel):+ def __init__(self, parent, id, frame, file): wxPanel.__init__(self, parent, -1) self.html = exHtmlWindow(self, -1, frame) self.box = wxBoxSizer(wxVERTICAL) self.box.Add(self.html, 1, wxGROW) self.SetSizer(self.box) self.SetAutoLayout(true)+ self.html.LoadPage(file)
To make it more independent and to make it more like a browser, we will extend the ttHtmlPanel class to add buttons for executing standard browser tasks. Of course, if you plan to build a real browser application, you may have to consider more about GUI design and availability than we do here.
Listing 3. modify ttHtmlPanel to add a button
class ttHtmlPanel(wxPanel): def __init__(self, parent, id, frame): wxPanel.__init__(self, parent, -1) self.frame = frame self.cwd = os.path.split(sys.argv[0])[0] if not self.cwd: self.cwd = os.getcwd self.html = ttHtmlWindow(self, -1, self.frame) self.box = wxBoxSizer(wxVERTICAL) self.box.Add(self.html, 1, wxGROW) subbox = wxBoxSizer(wxHORIZONTAL) btn = wxButton(self, 1202, "Load File") EVT_BUTTON(self, 1202, self.OnLoadFile) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1203, "Load Page") EVT_BUTTON(self, 1203, self.OnLoadPage) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1204, "Back") EVT_BUTTON(self, 1204, self.OnBack) subbox.Add(btn, 1, wxGROW | wxALL, 2) btn = wxButton(self, 1205, "Forward") EVT_BUTTON(self, 1205, self.OnForward) subbox.Add(btn, 1, wxGROW | wxALL, 2) self.box.Add(subbox, 0, wxGROW) self.SetSizer(self.box) self.SetAutoLayout(true) def OnLoadPage(self, event): dlg = wxTextEntryDialog(self, 'Location:') if dlg.ShowModal() == wxID_OK: self.destination = dlg.GetValue() dlg.Destroy() self.html.LoadPage(self.destination) def OnLoadFile(self, event): dlg = wxFileDialog(self, wildcard = '*.htm*', style=wxOPEN) if dlg.ShowModal(): path = dlg.GetPath() self.html.LoadPage(path) dlg.Destroy() def OnBack(self, event): if not self.html.HistoryBack(): wxMessageBox("No more items in history!") def OnForward(self, event): if not self.html.HistoryForward(): wxMessageBox("No more items in history!")
If you have used wxPython or any other Python graphical toolbox before, you can find that all we do is add another container to the panel and place the four buttons in it, callback function with the method added in the exHtmlPanel class. The basic wxHtml class cleverly manages the history for us. Therefore, OnBack and OnForward are only calls to the basic method.
Assuming that you have been using the Python interpreter when reading this, you may note that if you close the application, it never returns the control to the console. This problem is easy to solve, but we may need to add a menu bar to provide a file menu with exit options:
Listing 4. modifying exFrame to add a file menu with exit
class exFrame(wxFrame): def __init__(self, parent, ID, title): wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(600,750)) panel = exHtmlPanel (self, -1, self) mnu_file = wxMenu() mnu_file.Append(101, "E&xit", "Exit the browser") menuBar = wxMenuBar() menuBar.Append(mnu_file, "F&ile") self.SetMenuBar(menuBar) EVT_MENU(self, 101, self.Exit) def Exit(self, event): self.Close(true)
When we didn't try to change it into a real browser, we found that two additional items were missing at the end: most browsers have status bars, and you may notice that no image is drawn. The following changes to exApp, exFrame, and exHtmlPanel are added with a status bar and all built-in image support from wxPython:
Listing 5. adding status bar and image support
class exApp(wxApp): def OnInit(self):+ wxInitAllImageHandlers() frame = exFrame(NULL, -1, "Example Browser") frame.Show(true) self.SetTopWindow(frame) return trueclass exHtmlPanel(wxPanel): def __init__(self, parent, id, frame): wxPanel.__init__(self, parent, -1) self.frame = frame self.cwd = os.path.split(sys.argv[0])[0] if not self.cwd: self.cwd = os.getcwd self.html = exHtmlWindow(self, -1, self.frame)+ self.html.SetRelatedFrame(self.frame, "%s")+ self.html.SetRelatedStatusBar(0)...class exFrame(wxFrame): def __init__(self, parent, ID, title): wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(600,750)) panel = exHtmlPanel (self, -1, self)+ self.CreateStatusBar()+ self.SetStatusText("Default status bar")...
Now, the basic browser functions should be complete. WxPython advanced features allow you to create your own tags. you can customize code to process these tags to perform any operations you choose. The control of your customized embedded browser provides unlimited possibilities for enhanced report generation and online help.
The code itself can easily provide the foundation for any number of applications, and there is no reason to limit you to providing only online help. Please use these classes freely to see what interesting behavior can happen to them. :-)