簡體   English   中英

TypeError: QWebEngineView(parent: QWidget = None): 參數 1 有意外類型'PySide2.QtWidgets.QFrame'

[英]TypeError: QWebEngineView(parent: QWidget = None): argument 1 has unexpected type 'PySide2.QtWidgets.QFrame'

我目前正在嘗試使用提升的 QtWidget 在我的 PyQT5 腳本中嵌入QtWebEngine ,但我無法讓它正常工作。

這是我的代碼:

import sys
import os
import platform
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, uic

# UI File
from harvester_ui import Window
from ui_functions import * 


class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Window()
        self.ui.setupUi(self)


        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        
        self.ui.minimize.clicked.connect(lambda: self.showMinimized())
        self.ui.close.clicked.connect(app.exit)
        
        def moveWindow(w): 
            if w.buttons() == Qt.LeftButton:  
                self.move(self.pos() + w.globalPos() - self.clickPosition)
                self.clickPosition = w.globalPos()
                w.accept()

        self.ui.label.mouseMoveEvent = moveWindow

        self.show()

    def mousePressEvent(self, event):
        self.clickPosition = event.globalPos()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

如果我嘗試執行此代碼,則會收到標題中顯示的錯誤。

如果我用這些替換最后幾行,它將正常工作,但它不會像我想要的那樣 function 因為我的更改存儲在 class 中。

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    path_ui = os.path.join(os.path.dirname(__file__), "window.ui")
    window = uic.loadUi(path_ui)
    window.browser.load(QtCore.QUrl("https://google.com/"))
    window.show()
    sys.exit(app.exec_())

我的目標是能夠直接從我正在使用的 class 嵌入瀏覽器實例,而不是在 if 語句中進行。

盡管與 Qt、PyQt 和 PySide 的綁定幾乎相同,但絕不應該一起使用。 當嘗試將使用綁定創建的小部件重新設置為使用另一個綁定創建的小部件時,大多數問題都會出現,就像您的情況一樣。

解決方案很簡單:您可以使用 PyQt5PySide2。

使用 uic 工具生成的文件( pyuic為 pyuic,PySide 為pyside2-uic )時,必須使用相關綁定。 如果pyuic (即 PyQt 工具),則導入必須使用 PyQt5; 如果文件是使用pyside2-uic生成的,則必須改用 PySide2。

導入時您還應該更加小心,因為不同“級別”的導入可能會導致混淆。

例如,在您的情況下,以下行將導致從 PySide 導入QtWidgets.QMainWindow

from PySide2 import QtCore, QtGui, QtWidgets

以下使QMainWindow也使用 PySide 版本:

from PySide2.QtWidgets import *

這將覆蓋第一個導入,這將使QtWidgets.QMainWindow改用 PyQt 版本:

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, uic

使用上面的導入語句,結果是QtWidgets.QMainWindow將來自 PyQt5,而QMainWindow將來自 PySide2(對於每個子模塊的所有其他類都相同)。
即使忽略這兩個模塊不應該一起使用的事實,從不同模塊導入相同的 class 名稱也是一個嚴重的語義問題,肯定會導致錯誤。

使用連貫導入 styles(至少在從同一主模塊導入時)通常是可取的。 例如,以下幾行:

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PySide2.QtWidgets import *

它們是相關但不同的導入 styles,但最重要的是顯式導入(第二行和第三行)和最后一行的通配符導入使第一行幾乎無用。
考慮到雖然在 python 中通常更喜歡顯式(參見Python 的 Zen ),但每個規則都有其例外,對於像 Qt 這樣的非常大的模塊,它也會變得非常不舒服考慮到在生產過程中,一些類可能會變得未使用,但仍然不必要地導入,因為它們從未從導入中刪除。

由於加載期間的性能差異通常可以忽略不計,因此使用子模塊導入通常更可取(而不是顯式或通配符導入):它避免了可能非常長的導入語句(如顯式那樣),並且它還可以更清楚地從哪個子模塊已加載 class。

Tensorflow TypeError: Fetch argument None has invalid type <type 'n< div><div id="text_translate"><pre> a=tf.Variable(0, name='input') b=tf.constant(1) mid_val =tf.add(a,b) update_value =tf.compat.v1.assign(a,mid_val) tg=initialize_all_variables() with tf.compat.v1.Session() as sess: sess.run(tg) print(sess.run(a)) for i in range(3): sess.run(update_value) print(sess.run(a))</pre><p> 它在sess.run(tg)中給我錯誤並引發類型錯誤,在運行時它給出以下錯誤:</p><pre> &lt;ipython-input-20-fd2283cdd3bd&gt; in &lt;module&gt;() 10 11 with tf.compat.v1.Session() as sess: ---&gt; 12 sess.run(tg) 13 print(sess.run(a)) 14 3 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 263 """ 264 if fetch is None: --&gt; 265 raise TypeError(f'Argument `fetch` = {fetch} has invalid type ' 266 f'"{type(fetch).__name__}". Cannot be None') 267 elif isinstance(fetch, (list, tuple)):</pre><p> 我應該怎么辦?</p></div></type>

[英]Tensorflow TypeError: Fetch argument None has invalid type <type 'N

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 TypeError: QTableView(parent: QWidget = None): 參數 1 具有意外類型“int” TypeError: arguments 不匹配任何重載調用:QLineEdit(parent: QWidget = None): argument 1 has unexpected type 'Ui_EncryptionbyPyQt5' l QHBoxLayout(QWidget):參數1具有意外的類型&#39;QVBoxLayout&#39; QFrame不以QWidget為父級嗎? TypeError:QPixmap():參數 1 具有意外類型“Figure” TypeError:參數 1 具有意外類型“NoneType” TypeError:參數 1 具有意外類型“QPushButton” Tensorflow TypeError:Fetch參數None具有無效類型 <type 'NoneType'> ? Tensorflow TypeError: Fetch argument None has invalid type <type 'n< div><div id="text_translate"><pre> a=tf.Variable(0, name='input') b=tf.constant(1) mid_val =tf.add(a,b) update_value =tf.compat.v1.assign(a,mid_val) tg=initialize_all_variables() with tf.compat.v1.Session() as sess: sess.run(tg) print(sess.run(a)) for i in range(3): sess.run(update_value) print(sess.run(a))</pre><p> 它在sess.run(tg)中給我錯誤並引發類型錯誤,在運行時它給出以下錯誤:</p><pre> &lt;ipython-input-20-fd2283cdd3bd&gt; in &lt;module&gt;() 10 11 with tf.compat.v1.Session() as sess: ---&gt; 12 sess.run(tg) 13 print(sess.run(a)) 14 3 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in for_fetch(fetch) 263 """ 264 if fetch is None: --&gt; 265 raise TypeError(f'Argument `fetch` = {fetch} has invalid type ' 266 f'"{type(fetch).__name__}". Cannot be None') 267 elif isinstance(fetch, (list, tuple)):</pre><p> 我應該怎么辦?</p></div></type> TypeError:獲取參數None具有無效的類型<type 'NoneType'>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM