基于pyqt的无边框浏览器

自己写了个基于pyqt的无边框浏览器,可以配合源阅读的web看书,自用自取
目录结构:

  • ui
    |-- ui_webview.py
  • call_webview.y

环境安装:

pip install PyQt5
pip install PyQtWebEngine

我的本地环境:
python == 3.7
PyQt5 == 5.15.9
PyQtWebEngine == 5.15.6

直接执行 call_webview.py 即可使用,网址修改 call_webview 中对应位置即可,源码如下:

ui_webview.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_WebView(object):
    def setupUi(self, WebView):
        WebView.setObjectName("WebView")
        WebView.setWindowModality(QtCore.Qt.NonModal)
        WebView.resize(538, 276)
        WebView.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        WebView.setAnimated(True)
        self.centralwidget = QtWidgets.QWidget(WebView)
        self.centralwidget.setObjectName("centralwidget")
        WebView.setCentralWidget(self.centralwidget)
        self.statusBar = QtWidgets.QStatusBar(WebView)
        self.statusBar.setObjectName("statusBar")
        WebView.setStatusBar(self.statusBar)

        self.retranslateUi(WebView)
        QtCore.QMetaObject.connectSlotsByName(WebView)

    def retranslateUi(self, WebView):
        _translate = QtCore.QCoreApplication.translate
        WebView.setWindowTitle(_translate("WebView", " "))

call_webview.py

from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QMainWindow, QHBoxLayout
from PyQt5.QtCore import Qt, QPoint, QUrl
import ui.ui_webview as ui


class WebViewWindow(QMainWindow, ui.Ui_WebView):
    def __init__(self):
        super(WebViewWindow, self).__init__()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setupUi(self)
        self.webview = WebEngineView(self)
        self.webview.load(QUrl("http://手机ip地址/bookshelf/index.html#/"))
        
        # 渲染到页面
        self.setCentralWidget(self.webview)

    def mouseMoveEvent(self, e: QMouseEvent):  # 重写移动事件
        self._endPos = e.pos() - self._startPos
        self.move(self.pos() + self._endPos)

    def mousePressEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._isTracking = True
            self._startPos = QPoint(e.x(), e.y())

    def mouseReleaseEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._isTracking = False
            self._startPos = None
            self._endPos = None

# 创建浏览器,重写重写createwindow方法实现页面连接的点击跳转
class WebEngineView(QWebEngineView):

  def __init__(self, mainwindow, parent=None):
    super(WebEngineView, self).__init__(parent)
    self.mainwindow = mainwindow

  # 重写createwindow()
  def createWindow(self, QWebEnginePage_WebWindowType):
    new_webview = WebEngineView(self.mainwindow)
    self.mainwindow.create_tab(new_webview)
    return new_webview


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    mainWindow = WebViewWindow()
    mainWindow.show()
    sys.exit(app.exec_())

你可能感兴趣的:(实用配置,pyqt)