The development tools

Python version: 3.5.4 Related modules: PyQt5 module (version 5.10) and some built-in Python modules.

The main idea

Main functions:

(1) Web pages can be displayed normally;

(2) Set the navigation bar to realize the forward, backward, stop loading and refreshing functions of the browser;

(3) Set the address bar, you can update the URL of the current web page in real time, and support the access function of entering the address enter;

(4) Set the TAB page, you can double-click the navigation bar to open the new TAB page, so as to support access to multiple pages at the same time. The implementation of the underlying transport protocol and other functions: with the help of PyQt5 QtWebEngine module.

Dependency installation

Install PyQt5

Qt is a cross-platform C++ application development framework

1|sudo apt-get install python3-pyqt5
Copy the code

After the installation is complete, go to the Python cli to check whether the installation is correct

1|python3  
2|>>>import PyQt5
Copy the code

If no message is displayed after the command is executed, the installation is successful

Programming to realize

Qt provides the QtWebKit module for developers,QtWebKit is a web content rendering engine based on the open source project WebKit, with the help of the engine can be more quickly integrate the Universal web into Qt applications.

IO/Archives /qt…

## The browser has a window that can be used to display web pages

### Create browser

Qt’s program enters an event loop by creating an instance of the QApplication class to call the exec_() method. The program then loops around listening for various events and putting them into a message queue, pulling them out of the queue for processing when appropriate.

1| Create an application by creating an instance of the QApplication class
2|app = QApplication(sys.argv)
3|Run the application and loop to listen for events
4|app.exec_()  
Copy the code

We can create toolbars using the QToolBar provided by Qt

1|...
2|# Add navigation bar
3|navigation_bar = QToolBar('Navigation')
4|Set the size of the icon
5|navigation_bar.setIconSize(QSize(16.16))
6|# Add navigation bar to window
7|self.addToolBar(navigation_bar)
8|...

Copy the code

The QAction class provides abstract user interface actions

1|# add button
2|reload_button = QAction(QIcon('icons/renew.png'), 'reload', self)
Copy the code

Bind the action to the actual function

1|reload_button.triggered.connect(self.browser.reload)
Copy the code

These actions can be placed in widgets

1|navigation_bar.addAction(reload_button)
Copy the code

Qt has a powerful widget class, QWidgets, from which many other widgets can be derived. For example, QLineEdit is a single-line text box, which is used as an address bar to add an address bar for browsing

1|# add URL address bar
2|self.urlbar = QLineEdit()
Copy the code

Each component in Qt has a signal mechanism that can be used to bind the signal to the corresponding handler. For example, the address bar carriage return signal urlbar.returnPressed is bound to the navigate_to_url function. Navigate_to_url is triggered when the address bar return signal is sent

1|Make the address bar responsive to the enter key signal
2|self.urlbar.returnPressed.connect(self.navigate_to_url)
3|# navigate_to_url function
4|def navigate_to_url(self) :
5|   q = QUrl(self.urlbar.text())
6|    if q.scheme() == ' ':
7|        q.setScheme('http')
8|    self.browser.setUrl(q)
Copy the code

code

# v1.2
# created
# by Roger
# in 2017.1.3

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebKitWidgets import *

import sys

class MainWindow(QMainWindow) :
    # noinspection PyUnresolvedReferences
    def __init__(self, *args, **kwargs) :
        super().__init__(*args, **kwargs)
        # Set the window title
        self.setWindowTitle('My Browser')
        Set the window icon
        self.setWindowIcon(QIcon('icons/penguin.png'))
        Set the window size to 900 x 600
        self.resize(900.600)
        self.show()

        # Set browser
        self.browser = QWebView()
        url = 'http://blog.csdn.net/roger_lzh'
        # specify the URL to open the interface
        self.browser.setUrl(QUrl(url))
        Add a browser to the window
        self.setCentralWidget(self.browser)


        ### Create the navigation bar using QToolBar, and create the button using QAction
        # Add navigation bar
        navigation_bar = QToolBar('Navigation')
        Set the size of the icon
        navigation_bar.setIconSize(QSize(16.16))
        # Add navigation bar to window
        self.addToolBar(navigation_bar)

        The #QAction class provides abstract user interface actions that can be placed in widgets
        # Add forward, backward, stop loading and refresh buttons
        back_button = QAction(QIcon('icons/back.png'), 'Back', self)
        next_button = QAction(QIcon('icons/next.png'), 'Forward', self)
        stop_button = QAction(QIcon('icons/cross.png'), 'stop', self)
        reload_button = QAction(QIcon('icons/renew.png'), 'reload'. self) back_button.triggered.connect(self.browser.back) next_button.triggered.connect(self.browser.forward) stop_button.triggered.connect(self.browser.stop) reload_button.triggered.connect(self.browser.reload)Add the button to the navigation bar
        navigation_bar.addAction(back_button)
        navigation_bar.addAction(next_button)
        navigation_bar.addAction(stop_button)
        navigation_bar.addAction(reload_button)

        # add URL address bar
        self.urlbar = QLineEdit()
        Make the address bar responsive to the enter key signal
        self.urlbar.returnPressed.connect(self.navigate_to_url)

        navigation_bar.addSeparator()
        navigation_bar.addWidget(self.urlbar)

        Let the browser change the url accordingly
        self.browser.urlChanged.connect(self.renew_urlbar)

    def navigate_to_url(self) :
        q = QUrl(self.urlbar.text())
        if q.scheme() == ' ':
            q.setScheme('http')
        self.browser.setUrl(q)

    def renew_urlbar(self, q) :
        Update the current page link to the address bar
        self.urlbar.setText(q.toString())
        self.urlbar.setCursorPosition(0)


# create an app
app = QApplication(sys.argv)
Create the main window
window = MainWindow()
# display window
window.show()
Run the application and listen for events
app.exec_()    
Copy the code

That concludes this article. Thank you for watching. Follow me in my daily series of Python widgets

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ Complete source code + dry plus Python novice learning exchange community: 594356095