讓我們開發一個瀏覽器(二)
阿新 • • 發佈:2018-12-12
讓我們開發一個瀏覽器(一)實現了在一個標籤中跳轉的建議瀏覽器,接下來新增一些功能,如位址列、多標籤、前進、後退、重新整理...
實現效果如下:
原始碼如下:
from PyQt5.QtCore import QUrl, pyqtSignal, Qt from PyQt5.QtGui import QMovie, QIcon from PyQt5.QtWidgets import QMainWindow from PyQt5.QtWebEngineWidgets import QWebEngineView from ui.main_ui import Ui_MainWindow class NewQWebEngineView(QWebEngineView): new_signal = pyqtSignal(object) def __init__(self): super(NewQWebEngineView, self).__init__() def createWindow(self, QWebEnginePage_WebWindowType=None): browser = NewQWebEngineView() browser.resize(self.size()) #傳送新建的browser self.new_signal.emit(browser) return browser class MyBrowser(QMainWindow, Ui_MainWindow): def __init__(self): super(MyBrowser, self).__init__() self.setupUi(self) self.is_first = True self.pre_index = 0 self.browsers = {} self.url = 'http://www.baidu.com' # 初始化 self._init() def _init(self): # 清除原有的兩個標籤 self.tabWidget.clear() self.tabWidget.tabCloseRequested.connect(self.tab_close) # 例項化NewQWebEngineView browser = NewQWebEngineView() browser.new_signal.connect(self.new_tab) browser.createWindow() #視窗最大化 self.setWindowState(Qt.WindowMaximized) #設定動態圖示 self.pm = QMovie(':/icon/images/title.gif') self.pm.start() self.pm.frameChanged.connect(lambda: self.setWindowIcon(QIcon(self.pm.currentPixmap()))) # 槽函式 self.tabWidget.currentChanged.connect(self.tabWidget_currentChanged) self.lineEdit_url.returnPressed.connect(self.lineEdit_url_returnPressed) def lineEdit_url_returnPressed(self): browser = NewQWebEngineView() browser.setUrl(QUrl(self.lineEdit_url.text())) self.new_tab(browser) def tabWidget_currentChanged(self, _index): try: self.pushButton_back.clicked.disconnect(self.browsers[self.pre_index].back) self.pushButton_forward.clicked.disconnect(self.browsers[self.pre_index].forward) self.pushButton_refresh.clicked.disconnect(self.browsers[self.pre_index].reload) except: pass self.pushButton_back.clicked.connect(self.browsers[_index].back) self.pushButton_forward.clicked.connect(self.browsers[_index].forward) self.pushButton_refresh.clicked.connect(self.browsers[_index].reload) self.lineEdit_url.setText(self.browsers[_index].url().url()) def update_url(self, index, browser, is_finish=True): if is_finish: self.tabWidget.setTabText(index, browser.page().title()[0:20]) self.lineEdit_url.setText(browser.url().url()) else: self.tabWidget.setTabText(index, browser.url().url()[0:20]) def tab_close(self, i): self.tabWidget.removeTab(i) if self.tabWidget.count() == 0: self.close() def new_tab(self, browser): if self.is_first: browser.setUrl(QUrl(self.url)) self.is_first = False browser.new_signal.connect(self.new_tab) # 新增標籤,返回索引 _index = self.tabWidget.addTab(browser, '載入中...') self.browsers[_index] = browser self.tabWidget.setCurrentIndex(_index) self.pre_index = _index # url變化後改變tab標題 browser.urlChanged.connect(lambda: self.update_url(_index, browser, is_finish=False)) # 載入完成之後將標籤標題修改為網頁相關的標題 browser.loadFinished.connect(lambda: self.update_url(_index, browser, is_finish=True)) if __name__ == '__main__': import sys from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) my_browser = MyBrowser() my_browser.show() app.exec_()