简体   繁体   中英

QWebEngineView and QWidget in PyQt5

Python 3.6.1 PyQt5 (5.8.2) OS: Windows Ask: I want to open site in programm and show botton in top or maybe not bottom, but i don't show nothing from QWidgets if i show QWebEngineView, and don't show QWebEngineView if QWidgets. How can i show QWebEngineView and QWidget in 1 window? My code:

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineView, QWebEnginePage
from PyQt5.QtGui import QIcon
import PyQt5
import sys

class MyBrowser(QWebEnginePage):

    def userAgentForUrl(self, url):
        return "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

class Web(QWebEngineView):

    def load(self, url):
        self.setUrl(QUrl(url))

    def adjustTitle(self):
        self.setWindowTitle(self.title())

    def disableJS(self):
        settings = QWebEngineSettings.globalSettings()
        settings.setAttribute(QWebEngineSettings.JavascriptEnabled, False)

class Main(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Name')
        self.setWindowIcon(QIcon('icon.png'))

        self.btn = QPushButton('Button', self)
        self.btn.resize(self.btn.sizeHint())
        self.btn.move(20, 20)
        self.show()

app = QApplication(sys.argv)
web = Web()
main = Main()
web.load("https://google.com")
web.show()
app.exec_()

You must use a layout to place, QWebEngineView and its childrens classes are QWidgets

class Main(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Name')
        self.setWindowIcon(QIcon('icon.png'))

        web = Web()

        web.load("https://google.com")

        self.btn = QPushButton('Button', self)
        self.btn.resize(self.btn.sizeHint())
        lay = QVBoxLayout(self)
        lay.addWidget(self.btn)
        lay.addWidget(web)

app = QApplication(sys.argv)
main = Main()
main.show()
app.exec_()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM