当前位置:网站首页>Pyside2 as a simple browser
Pyside2 as a simple browser
2022-07-22 15:32:00 【fresh_ nam】
List of articles
Preface
Recently, I learned from the tutorial of Bai Yue Heiyu pyside2, Learn the embedded browser part , His demo The code can only be added when clicking the link tab Page function . So what I learned is just for him demo Code modification , Make it more like a browser .
One 、 Add functionality
1、 Add one url Input box , After entering the website address, click enter to jump to the corresponding page
2、 Every tab Added a close button
Two 、 Realization
One 、 Interface and function code
Interface code , First add an input box control and tab Control , In order for the control to follow The page zooms in and out , So the page should be set to vertical layout ( Upper and lower layout )
class MainWindow(QMainWindow):
# initialization
def __init__(self):
super(MainWindow, self).__init__()
# Set the initialization size of the window
self.resize(1000, 800)
self.centralwidget = QWidget(self)
self.centralwidget.setObjectName("centralwidget")
# Setting up layout , Add to the overall layout
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
# Web page input box control
self.web_url_edt = QLineEdit(self)
self.web_url_edt.setObjectName("web_url_edt")
# Add a web page input box to the layout
self.verticalLayout.addWidget(self.web_url_edt)
# tab Control
self.web_tabWidget = QTabWidget(self)
# tab Add close button
self.web_tabWidget.setTabsClosable(True)
self.web_tabWidget.setObjectName("web_tabWidget")
# tab Box is added to the layout
self.verticalLayout.addWidget(self.web_tabWidget)
# Set the overall layout of the form
self.setCentralWidget(self.centralwidget)
Here we will use pyside2 Medium QWebEnginePage To visit the website , The kernel is the same as Google browser , Create a class to specifically access websites and create new windows
class HtmlView(QWebEngineView):
# initialization
def __init__(self, *args, **kwargs):
# Initializes the parent class
QWebEngineView.__init__(self, *args, **kwargs)
# Call the parent class QObject Of parent() Method
self.tab = self.parent()
# Visit the website and create a window
def createWindow(self, windowType):
if windowType == QWebEnginePage.WebBrowserTab:
webView = HtmlView(self.tab)
ix = self.tab.addTab(webView, " Loading ...")
self.tab.setCurrentIndex(ix)
return webView
return QWebEngineView.createWindow(self, windowType)
For a better user experience , So create a web page first when initializing , The complete code is as follows
tabedBrowserTab.py
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Set the initialization size of the window
self.resize(1000, 800)
self.centralwidget = QWidget(self)
self.centralwidget.setObjectName("centralwidget")
# Setting up layout , Add to the overall layout
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
# Web page input box control
self.web_url_edt = QLineEdit(self)
self.web_url_edt.setObjectName("web_url_edt")
# Add a web page input box to the layout
self.verticalLayout.addWidget(self.web_url_edt)
# tab Control
self.web_tabWidget = QTabWidget(self)
# tab Add close button
self.web_tabWidget.setTabsClosable(True)
self.web_tabWidget.setObjectName("web_tabWidget")
# tab Box is added to the layout
self.verticalLayout.addWidget(self.web_tabWidget)
# Set the overall layout of the form
self.setCentralWidget(self.centralwidget)
# encapsulation url
self.url = QUrl("https://www.baidu.com")
# Instantiation HtmlView
view = HtmlView(self.web_tabWidget)
# Call the parent class QWebEngineView Of load Method loading url
view.load(self.url)
# Call the superclass method QTabWidget Of addTab Method to add a new tab page
ix = self.web_tabWidget.addTab(view, "https://www.baidu.com")
self.web_url_edt.returnPressed.connect(self.to_web) # Listen for the enter button click event in the input box , Click to execute to_web function
self.web_tabWidget.tabCloseRequested.connect(self.close_tab) # Listen for the close button click event , Click to execute close_tab function
# Create a new tab Page and visit
def to_web(self):
self.url = QUrl("https://" + self.web_url_edt.text())
# Instantiation HtmlView
view = HtmlView(self.web_tabWidget)
# Call the parent class QWebEngineView Of load Method loading url
view.load(self.url)
# Call the superclass method QTabWidget Of addTab Method to add a new tab page
ix = self.web_tabWidget.addTab(view, "https://" + self.web_url_edt.text())
self.web_tabWidget.setCurrentIndex(ix) # Jump to the current tab page
# close tab page
def close_tab(self, index):
self.web_tabWidget.removeTab(index) # close tab page
class HtmlView(QWebEngineView):
def __init__(self, *args, **kwargs):
# Initializes the parent class
QWebEngineView.__init__(self, *args, **kwargs)
# Call the parent class QObject Of parent() Method
self.tab = self.parent()
def createWindow(self, windowType):
if windowType == QWebEnginePage.WebBrowserTab:
webView = HtmlView(self.tab)
ix = self.tab.addTab(webView, " Loading ...")
self.tab.setCurrentIndex(ix)
return webView
return QWebEngineView.createWindow(self, windowType)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
# Add an icon to the window
app.setWindowIcon(QIcon('network.ico'))
# main = TabWidget()
main = MainWindow()
main.show()
sys.exit(app.exec_())
Two 、 Running results
Visit your blog address , It can be displayed normally like a browser ( Don't write the URL https://, The code has been automatically supplemented )
Clicking the link will also jump to a new page
3、 ... and 、 problem
When you click on a link on a web page , Because the link cannot be extracted url, The title can only be loading , If there is a solution, please leave a message in the comment area .
边栏推荐
猜你喜欢
df.describe() 详解+用法+示例
[C language - program compilation] how does the line by line code get to an executable program step by step?
Architecture design scheme (continuously updating ing)
配置Eureka时Status显示的是电脑名而不是localhost及ipAddr显示为本机ip的问题
Introduction to microservices
Huawei cloud from entry to actual combat | AI cloud development modelarts entry and WAF application and deployment
TDengine實驗集群搭建 Success
21jvm memory model (JMM)
AutoLabel(自动标签)
nacos基础概念和单机启动
随机推荐
The difference between static variables and global variables
Equal protection compliance 2022 series | one center + triple protection, helping the construction of enterprise level protection to be more scientific
Shardingjdbc horizontal split table configuration
调试VBS visual studio
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or
DM8:查询达梦数据库数据文件使用大小限制
JDBC编程
Codeworks 5 questions per day (average 1500) - day 22
Two people line up to install locally & x360ce simulation handle Tutorial & xpadder handle simulation keyboard and mouse
golang语言cli库
JS高级 之 ES6 实现继承
上传图片到本机iis服务器,结果以网页地址形式返回,可直接访问
NFC Introduction (2)
重载(overload)和重写(override)的区别
电流探头应该如何选择
The installation and use of harbor+trivy -- the way to build a dream
MySQL Exercise one database Knowledge
【医学分割】Medical Image Segmentation Using Deep Learning: A Survey
JDBC programming
Tdengine learning notes