Display 9 echarts effects in Jiugongge mode

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
insert image description here

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. wxIt is the main module of the wxPython library, wx.html2the module is used to embed a web browser, webbrowserthe module is used to open an external browser, and osthe 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 Noneindicating that there is no parent window, titleindicating the title of the window, and sizeindicating the size of the window. Then, we create a panel self.panelwhich 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_pickerIs a folder selection control used to let the user select a folder. self.button_openis a button with the label "Open Page". self.browsersis a list that stores embedded web browser objects.

self.button_open.Bind(wx.EVT_BUTTON, self.on_open)

We use Bindmethods to bind the button's EVT_BUTTONevents with self.on_openmethods. This means that when the user clicks the button, self.on_opena 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.GridSizercreated a grid layout with 3 rows and 3 columns, and gapset the spacing between rows and columns through parameters. We then forcreated nine embedded web browser objects using a loop and added them to self.browsersa list. We use grid_sizer.Addmethods to add each browser panel to the Jiugongge layout. Next, we created a wx.BoxSizervertical layout manager and used Addmethods to add folder selection controls, nine grid layouts, and open webpage buttons to the vertical layout. Finally, we SetSizerapply 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_openThe 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 rangea 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.Appinstance of the condition, instantiate MainFramethe 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.

Guess you like

Origin blog.csdn.net/winniezhang/article/details/132332439