Home > other >  How can by reload_button button on each Tab page refresh tag
How can by reload_button button on each Tab page refresh tag

Time:12-31

Recently learn PyQt5 write browser, examples of the "refresh" button in the code only work on the first TAB, cannot refresh the second, third TAB,
Code of the self. The webview. Reload really only point to the first label,
New labels with the self. The tabWidget. CurrentWidget () can find a new TAB, but self. TabWidget. CurrentWidget. Reload () will go wrong,

 
# - * - coding: utf-8 - * -
# @ Author: DSHJ
# @ Date: 2020/04/26

The import sys
The from PyQt5. QtWidgets import *
The from PyQt5. QtCore import *
The from PyQt5. QtGui import *
The from PyQt5. QtWebEngineWidgets import QWebEngineView


# to create the main window
The class MainWindow (QMainWindow) :
Def __init__ (self, * args, * * kwargs) :
Super () __init__ (* args, * * kwargs)
# set the window title
Self. SetWindowTitle (' simple browser)
# set the window size 900 * 600
The self. The resize (1300, 700)
The self. The show ()

# to create tabwidget (tabbed page)
Self. TabWidget=QTabWidget ()
Self. TabWidget. SetTabShape (QTabWidget. Triangular)
The self. The tabWidget. SetDocumentMode (True)
The self. The tabWidget. SetMovable (True)
The self. The tabWidget. SetTabsClosable (True)
Self. TabWidget. TabCloseRequested. Connect (self. Close_Tab)
Self. SetCentralWidget (self. TabWidget)

The first TAB page #
The self. The webview=WebEngineView (self) # self must have, is the main window as a parameter, to the browser
Self. Webview. Load (QUrl (" http://www.baidu.com "))
Self. Create_tab (self. Webview)

# use the QToolBar create navigation and use QAction create button
# add navigation
Navigation_bar=QToolBar (' Navigation ')
# set the size of the icon
Navigation_bar. SetIconSize (QSize (16, 16))
# a navigation bar is added to the window
Self. AddToolBar (navigation_bar)

# QAction class provides an abstract user interface action, the action can be placed in the widget
# add forward, backward, stop loading and refresh button
Back_button=QAction (QIcon (' ICONS/houtui. PNG '), 'Back', the self)
Next_button=QAction (QIcon (' ICONS/qianjin. PNG '), 'Forward', the self)
Stop_button=QAction (QIcon (' ICONS/close. PNG '), 'stop', the self)
Reload_button=QAction (QIcon (' ICONS/shuaxin. PNG '), 'reload' self)

# binding events
Back_button. Triggered. Connect (self. Webview. Back)
Next_button. Triggered. Connect (self. Webview. Forward)
Stop_button. Triggered. Connect (self. Webview. Stop)
Reload_button. Triggered. Connect (self. Webview. Reload)

# on the add button to the navigation
Navigation_bar. AddAction (back_button)
Navigation_bar. AddAction (next_button)
Navigation_bar. AddAction (stop_button)
Navigation_bar. AddAction (reload_button)

# add URL address bar
Self. Urlbar=QLineEdit ()
# for the address bar to response the enter key signal
The self. The urlbar. ReturnPressed. Connect (self. Navigate_to_url)

Navigation_bar. AddSeparator ()
Navigation_bar. AddWidget (self. Urlbar)

# the browser url address of the corresponding change
Self. Webview. UrlChanged. Connect (self. Renew_urlbar)

# show address
Def navigate_to_url (self) :
Q=QUrl (self. Urlbar. Text ())
If q.s cheme ()==":
Q.s etScheme (' HTTP ')
Self. Webview. SetUrl (q)

# response input address
Def renew_urlbar (self, q) :
# will link for the current page updated to address bar
The self. The urlbar. SetText (q.t oString ())
The self. The urlbar. SetCursorPosition (0)

# to create the TAB page
Def create_tab (self, webview) :
The self. The TAB=QWidget ()
The self. The tabWidget. AddTab (self) TAB, "new page")
Self. TabWidget. SetCurrentWidget (self. TAB)

# rendered to the page
The self. The Layout=QHBoxLayout (self. TAB)
Self. Layout. SetContentsMargins (0, 0, 0, 0)
Self. Layout. AddWidget (webview)

# close TAB page
Def close_Tab (self, index) :
If the self. The tabWidget. The count () & gt; 1:
The self. The tabWidget. RemoveTab (index)
The else:
The self. The close () # when only one TAB, close the main window


# to create a browser, rewriting rewrite createwindow method realize the connection between the page click jump
The class WebEngineView (QWebEngineView) :

Def __init__ (self, the mainwindow, parent=None) :
Super (WebEngineView, self) __init__ (parent)
The self. The mainwindow=mainwindow

# rewrite the createwindow ()
Def createWindow (self, QWebEnginePage_WebWindowType) :
New_webview=WebEngineView (self. Mainwindow)
The self. The mainwindow. Create_tab (new_webview)
Return new_webview


# program entry
If __name__=="__main__" :
App=QApplication (sys. Argv)
# to create the main window
Browser=MainWindow ()
The show ()
# running applications, and to monitor events
Sys. Exit (app. Exec_ ())

Hope god can teach
  • Related