Python+PyQt5! Implement a simple browser! do you use google? I use my own!

programming

Qt provides developers with the QtWebKit module, which is an open source project based on QtWebKit

WebKit's web page content rendering engine, which can be used to render the web page more quickly

Web integration into Qt applications.

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

Create a browser

The Qt program calls the exec_() method to enter the event loop by creating an instance of the QApplication class.

Then the program has been looping to listen to various events and put them into the message queue, and at the appropriate time from the queue

Column take out processing.

The QAction class provides abstract user interface actions

#Add button

reload_button = QAction(QIcon('icons/renew.png'), 'reload', self)

Bind the action to the actual function

reload_button.triggered.connect(self.browser.reload)

These actions can be placed in widgets

navigation_bar.addAction(reload_button)

There is a powerful widget class QWidgets in Qt, based on which many other widgets can be derived

, such as QLineEdit is a single-line text box, use this as the address bar, and add an address bar for browsing

#Add URL address bar

self.urlbar = QLineEdit()

Each component in Qt has a signal mechanism, which can be used to connect and bind signals with corresponding processing functions, such as

The carriage return signal urlbar.returnPressed of the address bar is bound to the navigate_to_url function, when the carriage return signal of the address bar is sent

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*600

self.resize(900, 600)

self.show()

# Set up the browser

self.browser = QWebView()

url = 'http://blog.csdn.net/roger_lzh'

# Specify the URL to open the interface

self.browser.setUrl(QUrl(url))

# add browser to window

self.setCentralWidget(self.browser)

###Create navigation bar with QToolBar and buttons with QAction

# add navigation bar

navigation_bar = QToolBar('Navigation')

# Set the size of the icon

navigation_bar.setIconSize(QSize(16, 16))

#Add the navigation bar to the window

self.addToolBar(navigation_bar)

The #QAction class provides abstract user interface actions that can be placed in widgets

# Add buttons for forward, backward, stop loading and refresh

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 respond 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 corresponding url address

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 link of the current web page to the address bar

self.urlbar.setText(q.toString())

self.urlbar.setCursorPosition (0)

# create application

app = QApplication(sys.argv)

# create the main window

window = MainWindow()

# show window

window.show()

# Run the application and listen for events

app.exec_()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313729&siteId=291194637