1

Python 3.6.1 PyQt5 (5.8.2) 操作系统:Windows 问:我想在程序中打开站点并在顶部或底部显示底部,但如果我显示 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_()
4

1 回答 1

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

在此处输入图像描述

于 2017-05-27T15:31:59.870 回答