欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5的QWebEngineView使用示例

 更新時間:2020年10月20日 16:00:08   作者:默默的點(diǎn)滴  
這篇文章主要介紹了PyQt5的QWebEngineView使用示例,幫助大家更好的學(xué)習(xí)和使用python,感興趣的朋友可以了解下

一.支持視頻播放

關(guān)鍵代碼

self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)  #支持視頻播放

二.支持頁面關(guān)閉請求

關(guān)鍵代碼

self.page().windowCloseRequested.connect(self.on_windowCloseRequested)  #頁面關(guān)閉請求

三.支持頁面下載請求

關(guān)鍵代碼

self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁面下載請求

完整源碼

【如下代碼,完全復(fù)制,直接運(yùn)行,即可使用】

import sys
import os
import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView,QWebEngineSettings
 
# 調(diào)試窗口配置
# 如果不想自己創(chuàng)建調(diào)試窗口,可以使用Chrome連接這個地址進(jìn)行調(diào)試
DEBUG_PORT = '5588'
DEBUG_URL = 'http://127.0.0.1:%s' % DEBUG_PORT
os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = DEBUG_PORT
 
################################################
#######創(chuàng)建主窗口
################################################
class MainWindow(QMainWindow):
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.setWindowTitle('My Browser')
    #self.showMaximized()
    self.setWindowFlags(Qt.FramelessWindowHint)
 
    #####創(chuàng)建tabwidget
    self.tabWidget = QTabWidget()
    self.tabWidget.setTabShape(QTabWidget.Triangular)
    self.tabWidget.setDocumentMode(True)
    self.tabWidget.setMovable(True)
    self.tabWidget.setTabsClosable(True)
    self.tabWidget.tabCloseRequested.connect(self.close_Tab)
    self.setCentralWidget(self.tabWidget)
 
    ####第一個tab
    self.webview = WebEngineView(self)  #self必須要有,是將主窗口作為參數(shù),傳給瀏覽器
    self.webview.load(QUrl("https://www.baidu.com"))
    self.create_tab(self.webview)
 
    #網(wǎng)頁調(diào)試窗口
    self.inspector = QWebEngineView()
    self.inspector.setWindowTitle('Web Inspector')
    self.inspector.load(QUrl(DEBUG_URL))
    self.webview.loadFinished.connect(self.handleHtmlLoaded)    
 
  # 加載完成后顯示調(diào)試網(wǎng)頁
  def handleHtmlLoaded(self, ok):
    if ok:
      self.webview.page().setDevToolsPage(self.inspector.page())
      self.inspector.show()
 
  #創(chuàng)建tab
  def create_tab(self,webview):
    self.tab = QWidget()
    self.tabWidget.addTab(self.tab, "新標(biāo)簽頁")
    self.tabWidget.setCurrentWidget(self.tab)
    #####
    self.Layout = QHBoxLayout(self.tab)
    self.Layout.setContentsMargins(0, 0, 0, 0)
    self.Layout.addWidget(webview)
 
  #關(guān)閉tab
  def close_Tab(self,index):
    if self.tabWidget.count()>1:
      self.tabWidget.removeTab(index)
    else:
      self.close()  # 當(dāng)只有1個tab時,關(guān)閉主窗口
 
################################################
#######創(chuàng)建瀏覽器
################################################
class WebEngineView(QWebEngineView):
 
  def __init__(self,mainwindow,parent=None):
    super(WebEngineView, self).__init__(parent)
    self.mainwindow = mainwindow
    ##############
    self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)   #支持視頻播放
    self.page().windowCloseRequested.connect(self.on_windowCloseRequested)   #頁面關(guān)閉請求
    self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁面下載請求
 
  # 支持頁面關(guān)閉請求
  def on_windowCloseRequested(self):
    the_index = self.mainwindow.tabWidget.currentIndex()
    self.mainwindow.tabWidget.removeTab(the_index)
 
 
  # 支持頁面下載按鈕
  def on_downloadRequested(self,downloadItem):
    if downloadItem.isFinished()==False and downloadItem.state()==0:
      ###生成文件存儲地址
      the_filename = downloadItem.url().fileName()
      if len(the_filename) == 0 or "." not in the_filename:
        cur_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        the_filename = "下載文件" + cur_time + ".xls"
      the_sourceFile = os.path.join(os.getcwd(), the_filename)
 
      ###下載文件
      # downloadItem.setSavePageFormat(QWebEngineDownloadItem.CompleteHtmlSaveFormat)
      downloadItem.setPath(the_sourceFile)
      downloadItem.accept()
      downloadItem.finished.connect(self.on_downloadfinished)
 
 
  # 下載結(jié)束觸發(fā)函數(shù)
  def on_downloadfinished(self):
    js_string = '''
    alert("下載成功,請到軟件同目錄下,查找下載文件!"); 
    '''
    self.page().runJavaScript(js_string)
 
 
  # 重寫createwindow()
  def createWindow(self, QWebEnginePage_WebWindowType):
    new_webview = WebEngineView(self.mainwindow)
 
    self.mainwindow.create_tab(new_webview)
 
    return new_webview
 
 
################################################
#######程序入門
################################################
if __name__ == "__main__":
  app = QApplication(sys.argv)
  the_mainwindow = MainWindow()
  the_mainwindow.show()
  sys.exit(app.exec())

以上就是PyQt5的QWebEngineView使用示例的詳細(xì)內(nèi)容,更多關(guān)于PyQt5的QWebEngineView的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼

    Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼

    相信大家應(yīng)該都知道Python繪制箱線圖主要用matplotlib庫里pyplot模塊里的boxplot()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python利用matplotlib.pyplot.boxplot()繪制箱型圖的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 解析python的局部變量和全局變量

    解析python的局部變量和全局變量

    函數(shù)內(nèi)部定義的變量就叫局部變量而如果一個變量既能在一個函數(shù)中使用,也可以在其他函數(shù)中使用,這樣的變量就是全局變量。 本文給大家介紹python的局部變量和全局變量的相關(guān)知識,感興趣的朋友一起看看吧
    2019-08-08
  • 利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實(shí)例

    利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 春節(jié)到了 教你使用python來搶票回家

    春節(jié)到了 教你使用python來搶票回家

    這篇文章主要介紹了春節(jié)到了 教你使用python來搶票回家,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • python基于event實(shí)現(xiàn)線程間通信控制

    python基于event實(shí)現(xiàn)線程間通信控制

    這篇文章主要介紹了python基于event實(shí)現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解

    Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Django 中的Timezone 處理操作

    Django 中的Timezone 處理操作

    這篇文章主要介紹了Django 中的Timezone 處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • numpy中的掩碼數(shù)組的使用

    numpy中的掩碼數(shù)組的使用

    本文主要介紹了numpy中的掩碼數(shù)組的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python SQLAlchemy入門教程(基本用法)

    Python SQLAlchemy入門教程(基本用法)

    這篇文章主要介紹了Python SQLAlchemy入門教程,本文通過實(shí)例主要給大家講解了python SQLAlchemy基本用法,需要的朋友可以參考下
    2019-11-11
  • pandas表連接 索引上的合并方法

    pandas表連接 索引上的合并方法

    今天小編就為大家分享一篇pandas表連接 索引上的合并方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論