The widget QWebView can be used to render webpages. QWebView uses the QtWebkit web browsing module. To show a website simply call the load(url) method.
QWebView puts a real webpage inside your PyQt window. It uses the
QtWebKit module, so the page renders with the same engine as the rest of Qt.
You load a url with load() and you get the usual navigation functions.
Note: QWebView only shows the webpage contents. Code for a python webbrowser below.
python webbrowser
In the example below we have a complete webbrowser. It supports tabs, basic navigation and a keyboard shortcut. It’s a minimal example that is extensible.
The look and feel depends on your operating system, you can make it look like Mac or Windows if you want. Thsi is is all with Python PyQt.
print(self.tabs.currentIndex()) if url.isValid(): self.tabWebView[self.tabs.currentIndex()].load(url) else: print("Url not valid") else: print("No tabs open, open one first.")
defcloseTab(self,tabId): print(tabId) del self.lNameLine[tabId] del self.tabWebView[tabId] self.tabs.removeTab(tabId)
defmain(): app = QApplication(sys.argv) ex = tabdemo() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
What QWebView gives you
load(url) opens a page.
back(), forward(), reload() and stop() handle history.
setUrl(text) reads the address bar and url() gives the current address.
setZoomFactor(1.5) makes the page bigger or smaller.
setJavaScriptEnabled(True) turns on javascript, many sites need it.
titleChanged lets you put the page title in your own window title.
For a plain browser you also want a QLineEdit for the address and a few buttons that
call the navigation methods.