Function:
A simple web browser application is created, a main window is created using the wxPython library, and nine web browser panels are embedded in the window. Users can select a folder and open multiple web pages with the click of a button, and each web page will be displayed in a separate web browser panel. This application provides a convenient way to view and compare the content of multiple web pages at the same time.
C:\pythoncode\blog\9ehartsshow.py
Summary:
When this code is executed, it creates a class called MainFrame, which inherits from wx.Frame, and is the main window of the application. Next, we will explain each part of the code in detail.
import wx
import wx.html2
import webbrowser
import os
First, we imported the necessary libraries. wx
It is the main module of the wxPython library, wx.html2
the module is used to embed a web browser, webbrowser
the module is used to open an external browser, and os
the module is used to handle file and folder operations.
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="Web Browser", size=(800, 600))
self.panel = wx.Panel(self)
Next, we define a class called MainFrame and __init__
initialize the main window in the constructor. We use super()
a function to call the constructor of the parent class, and pass some parameters, such as None
indicating that there is no parent window, title
indicating the title of the window, and size
indicating the size of the window. Then, we create a panel self.panel
which will contain our GUI components.
self.folder_picker = wx.DirPickerCtrl(self.panel, message="选择文件夹",
style=wx.DIRP_USE_TEXTCTRL | wx.DIRP_SMALL)
self.button_open = wx.Button(self.panel, label="打开网页")
self.browsers = []
In the constructor, we create three GUI components. self.folder_picker
Is a folder selection control used to let the user select a folder. self.button_open
is a button with the label "Open Page". self.browsers
is a list that stores embedded web browser objects.
self.button_open.Bind(wx.EVT_BUTTON, self.on_open)
We use Bind
methods to bind the button's EVT_BUTTON
events with self.on_open
methods. This means that when the user clicks the button, self.on_open
a method will be called to handle the event.
grid_sizer = wx.GridSizer(rows=3, cols=3, gap=(10, 10))
for _ in range(9):
browser = wx.html2.WebView.New(self.panel)
self.browsers.append(browser)
grid_sizer.Add(browser, proportion=1, flag=wx.EXPAND)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.folder_picker, flag=wx.EXPAND)
sizer.Add(grid_sizer, proportion=1, flag=wx.EXPAND)
sizer.Add(self.button_open, flag=wx.EXPAND)
self.panel.SetSizer(sizer)
In this part of the code, we create a nine grid layout and add nine web browser panels to it. We wx.GridSizer
created a grid layout with 3 rows and 3 columns, and gap
set the spacing between rows and columns through parameters. We then for
created nine embedded web browser objects using a loop and added them to self.browsers
a list. We use grid_sizer.Add
methods to add each browser panel to the Jiugongge layout. Next, we created a wx.BoxSizer
vertical layout manager and used Add
methods to add folder selection controls, nine grid layouts, and open webpage buttons to the vertical layout. Finally, we SetSizer
apply the vertical layout to the panel via method self.panel
.
def on_open(self, event):
folder_path = self.folder_picker.GetPath()
if not folder_path:
wx.MessageBox("请选择文件夹!", "错误", wx.OK | wx.ICON_ERROR)
return
html_files = [f for f in os.listdir(folder_path) if f.endswith(".html")]
num_browsers = min(len(html_files), 9)
for i in range(num_browsers):
html_file = os.path.join(folder_path, html_files[i])
browser = self.browsers[i]
browser.LoadURL(f"file://{
html_file}")
on_open
The method is to respond to the processing function of the click event of the button to open the webpage. This method will be called when the user clicks the button to open the page. First, it gets the folder path selected by the user. If no folder is selected, display an error dialog and return. Next, it lists all HTML files ending in ".html" in the folder and counts the number of pages that need to be opened (up to 9). Then, it uses range
a loop to traverse the number of web pages that need to be opened, and loads each HTML file into the corresponding web browser panel in turn.
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()
Finally, we __main__
create an wx.App
instance of the condition, instantiate MainFrame
the class, and show the main window. We then call app.MainLoop()
methods to run the application's event loop, causing the application to enter the main event loop and wait for user interaction.
Full code:
import wx
import wx.html2
import webbrowser
import os
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="Web Browser", size=(800, 600))
self.panel = wx.Panel(self)
# Create GUI components
self.folder_picker = wx.DirPickerCtrl(self.panel, message="选择文件夹",
style=wx.DIRP_USE_TEXTCTRL | wx.DIRP_SMALL)
self.button_open = wx.Button(self.panel, label="打开网页")
self.browsers = []
# Bind event handlers
self.button_open.Bind(wx.EVT_BUTTON, self.on_open)
# Create grid layout
grid_sizer = wx.GridSizer(rows=3, cols=3, gap=(10, 10))
for _ in range(9):
browser = wx.html2.WebView.New(self.panel)
self.browsers.append(browser)
grid_sizer.Add(browser, proportion=1, flag=wx.EXPAND)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.folder_picker, flag=wx.EXPAND)
sizer.Add(grid_sizer, proportion=1, flag=wx.EXPAND)
sizer.Add(self.button_open, flag=wx.EXPAND)
self.panel.SetSizer(sizer)
def on_open(self, event):
folder_path = self.folder_picker.GetPath()
if not folder_path:
wx.MessageBox("请选择文件夹!", "错误", wx.OK | wx.ICON_ERROR)
return
html_files = [f for f in os.listdir(folder_path) if f.endswith(".html")]
num_browsers = min(len(html_files), 9)
for i in range(num_browsers):
html_file = os.path.join(folder_path, html_files[i])
browser = self.browsers[i]
browser.LoadURL(f"file://{
html_file}")
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()
To summarize, this code creates a simple web browser application, using the wxPython library to create a main window with nine web browser panels embedded in the window. Users can select a folder and open multiple web pages with the click of a button, and each web page will be displayed in a separate web browser panel. This application provides a convenient way to view and compare the content of multiple web pages at the same time.