PyQt5中的QWebEngineView和QWidget

时间:2017-05-27 14:22:21

标签: python python-3.x pyqt pyqt5 qwebengineview

Python 3.6.1 PyQt5(5.8.2) 操作系统:Windows 问: 我想在程序中打开网站并在顶部或者不是底部显示botton,但如果我显示QWebEngineView,我不会显示QWidgets,如果QWidgets不显示QWebEngineView。如何在1个窗口中显示QWebEngineView和QWidget? 我的代码:

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_()

1 个答案:

答案 0 :(得分:2)

您必须使用布局放置,QWebEngineView及其子级别为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_()

enter image description here