Create a simple HTML Viewer application

Use wxPython and the embedded browser to create a simple HTML Viewer application.

In this article, we will use Python and the wxPython module to create a simple HTML Viewer application. This application allows the user to enter HTML content and displays the effect of that content in an embedded browser.
insert image description here

Preparation

Before we start, we need to make sure the following software and libraries are installed:

  • Python: We will use Python to write the application code. Make sure you have Python installed and can run the Python interpreter from the command line.

  • wxPython: This is a Python GUI development toolkit for creating desktop applications. We will use wxPython to build the user interface of the application. You can use the pip command to install wxPython:

pip install wxPython

Write code

First, we need to import the required libraries and modules, including the wx and wx.html2 modules. wx is the main module of wxPython, which is used to create windows and controls of the application, and the wx.html2 module is used to create the embedded browser.

import wx
import wx.html2 as webview

Next, we create a main window class MainFrame that inherits from wx.Frame. In the constructor __init__, we set the title and size of the window, and create a panel (panel) to accommodate other controls.

class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="HTML Viewer", size=(800, 600))
        self.panel = wx.Panel(self)

In the panel, we created three controls: a multi-line text box (memo), a button (button) and an embedded browser (web).

        self.memo = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
        self.button = wx.Button(self.panel, label="生成")
        self.web = webview.WebView.New(self.panel)

We bound the button's event to the on_generate method, which is called when the user clicks the button.

        self.button.Bind(wx.EVT_BUTTON, self.on_generate)

Then, we use wx.BoxSizer to set the layout of the panel. We use a vertical BoxSizer and add memo, buttons and web controls to the Sizer.

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.memo, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.button, flag=wx.EXPAND)
        sizer.Add(self.web, proportion=1, flag=wx.EXPAND)

        self.panel.SetSizer(sizer)

In the on_generate method, we get the HTML content in the memo and set it as the page content of the embedded browser.

    def on_generate(self, event):
        html_content = self.memo.GetValue()
        self.web.SetPage(html_content, "")

Then, we create a dialog (Dialog) and add the embedded browser to the layout of the dialog. Finally, we display the dialog.

        dialog = wx.Dialog(self, title="HTML Viewer")
        dialog.Sizer = wx.BoxSizer(wx.VERTICAL)
        dialog.Sizer.Add(self.web, proportion=1, flag=wx.EXPAND)
        dialog.ShowModal()

Finally, in the main program, we create a wx.App instance, and create a MainFrame instance, and display the main window.

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

run the application

After writing the code, we can run the application to test it. Execute the Python script on the command line and the main window of the application will be displayed. Enter some HTML content in the multi-line text box and click the Generate button. A new dialog box will pop up showing the effect of the HTML content just entered.

You can try to enter some HTML content, for example:

<!DOCTYPE html>
<html>
<head>
    <title>ECharts 交互功能示例</title>
    <!-- 引入 ECharts 的 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
</head>
<body>
    <!-- 用于显示图表的容器 -->
    <div id="chart-container" style="width: 600px; height: 400px;"></div>

    <script>
        // 初始化 ECharts 实例
        var chart = echarts.init(document.getElementById('chart-container'));

        // 配置图表数据和选项
        var options = {
      
      
            title: {
      
      
                text: '交互功能示例'
            },
            xAxis: {
      
      
                type: 'category',
                data: ['A', 'B', 'C', 'D', 'E']
            },
            yAxis: {
      
      
                type: 'value'
            },
            series: [{
      
      
                type: 'bar',
                data: [5, 20, 36, 10, 15]
            }]
        };

        // 使用配置项显示图表
        chart.setOption(options);

        // 添加交互功能:点击柱状图触发事件
        chart.on('click', function(params) {
      
      
            if (params.componentType === 'series') {
      
      
                // 获取点击的数据信息
                var dataIndex = params.dataIndex;
                var dataValue = params.value;

                // 在控制台输出点击的数据信息
                console.log('点击的数据索引:', dataIndex);
                console.log('点击的数据值:', dataValue);
            }
        });
    </script>
</body>
</html>

After clicking the generate button, a dialog box will pop up, in which the embedded browser will display the effect of the HTML content you input.
Full code:

import wx
import wx.html2 as webview


class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="HTML Viewer", size=(800, 600))
        self.panel = wx.Panel(self)
        self.memo = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
        self.button = wx.Button(self.panel, label="生成")
        self.web = webview.WebView.New(self.panel)

        self.button.Bind(wx.EVT_BUTTON, self.on_generate)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.memo, proportion=1, flag=wx.EXPAND)
        sizer.Add(self.button, flag=wx.EXPAND)
        sizer.Add(self.web, proportion=1, flag=wx.EXPAND)

        self.panel.SetSizer(sizer)

    def on_generate(self, event):
        html_content = self.memo.GetValue()
        self.web.SetPage(html_content, "")

        dialog = wx.Dialog(self, title="HTML Viewer")
        dialog.Sizer = wx.BoxSizer(wx.VERTICAL)
        dialog.Sizer.Add(self.web, proportion=1, flag=wx.EXPAND)
        dialog.ShowModal()


if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

This application is just a simple example, you can extend and customize it according to your needs. For example, you can add more features like saving and loading HTML files, exporting to PDF, etc.

Guess you like

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