如何使用PyQt5创建一个简单的浏览器。
Web浏览器是用于访问Internet上的信息的软件应用程序。当用户从特定网站请求网页时,网络浏览器会从网络服务器检索必要的内容,然后在屏幕上显示该页面。
PyQt5是跨平台的GUI工具包,是Qt v5的一组python绑定。由于该库提供的工具和简便性,因此可以轻松开发交互式桌面应用程序。
GUI的实现步骤:
1.创建一个主窗口
2.创建一个QWebEngineView对象并将其作为中央窗口小部件
添加到主窗口中3.将状态栏添加到主窗口中
4.创建一个工具栏并向其中添加导航按钮和行编辑显示网址,下面很热,工具栏看起来像
后端实现步骤:
1.更改URL后,将更新URL操作添加到QWebEngineView对象。
2.在更新url操作中,更改URL栏的URL并更改光标位置
3.加载完成后,向QWebEngineView对象添加另一个更新标题操作
。4.在更新标题方法中,将窗口的标题更新为页面标题
5 。使用QWebEngineView对象的内置功能将操作添加到导航按钮,以重新加载,后退,停止和前进按钮
6.将操作添加到主页按钮,然后在操作内部将网址更改为google.com。7
.将操作添加到按下回车键时的行编辑
8.在行编辑操作内部,获取文本并将其隐藏到QUrl对象,并设置方案(如果为null),然后将该URL设置为QWebEngineView对象
下面是实现
filter_none
亮度_4
# importing required libraries
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os
import sys
# creating main window class
class MainWindow(QMainWindow):
# constructor
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# creating a QWebEngineView
self.browser = QWebEngineView()
# setting default browser url as google
self.browser.setUrl(QUrl("http://google.com"))
# adding action when url get changed
self.browser.urlChanged.connect(self.update_urlbar)
# adding action when loading is finished
self.browser.loadFinished.connect(self.update_title)
# set this browser as central widget or main window
self.setCentralWidget(self.browser)
# creating a status bar object
self.status = QStatusBar()
# adding status bar to the main window
self.setStatusBar(self.status)
# creating QToolBar for navigation
navtb = QToolBar("Navigation")
# adding this tool bar tot he main window
self.addToolBar(navtb)
# adding actions to the tool bar
# creating a action for back
back_btn = QAction("Back", self)
# setting status tip
back_btn.setStatusTip("Back to previous page")
# adding action to the back button
# making browser go back
back_btn.triggered.connect(self.browser.back)
# adding this action to tool bar
navtb.addAction(back_btn)
# similarly for forward action
next_btn = QAction("Forward", self)
next_btn.setStatusTip("Forward to next page")
# adding action to the next button
# making browser go forward
next_btn.triggered.connect(self.browser.forward)
navtb.addAction(next_btn)
# similarly for reload action
reload_btn = QAction("Reload", self)
reload_btn.setStatusTip("Reload page")
# adding action to the reload button
# making browser to reload
reload_btn.triggered.connect(self.browser.reload)
navtb.addAction(reload_btn)
# similarly for home action
home_btn = QAction("Home", self)
home_btn.setStatusTip("Go home")
home_btn.triggered.connect(self.navigate_home)
navtb.addAction(home_btn)
# adding a separator in the tool bar
navtb.addSeparator()
# creating a line edit for the url
self.urlbar = QLineEdit()
# adding action when return key is pressed
self.urlbar.returnPressed.connect(self.navigate_to_url)
# adding this to the tool bar
navtb.addWidget(self.urlbar)
# adding stop action to the tool bar
stop_btn = QAction("Stop", self)
stop_btn.setStatusTip("Stop loading current page")
# adding action to the stop button
# making browser to stop
stop_btn.triggered.connect(self.browser.stop)
navtb.addAction(stop_btn)
# showing all the components
self.show()
# method for updating the title of the window
def update_title(self):
title = self.browser.page().title()
self.setWindowTitle("% s - Geek Browser" % title)
# method called by the home action
def navigate_home(self):
# open the google
self.browser.setUrl(QUrl("http://www.google.com"))
# method called by the line edit when return key is pressed
def navigate_to_url(self):
# getting url and converting it to QUrl objetc
q = QUrl(self.urlbar.text())
# if url is scheme is blank
if q.scheme() == "":
# set url scheme to html
q.setScheme("http")
# set the url to the browser
self.browser.setUrl(q)
# method for updating url
# this method is called by the QWebEngineView object
def update_urlbar(self, q):
# setting text to the url bar
self.urlbar.setText(q.toString())
# setting cursor position of the url bar
self.urlbar.setCursorPosition(0)
# creating a pyQt5 application
app = QApplication(sys.argv)
# setting name to the application
app.setApplicationName("Geek Browser")
# creating a main window object
window = MainWindow()
# loop
app.exec_()
输出:
Host List
hot news