I'm writing a script to load and display a QML file using the PySide2 library for python. Currently, the following script works:
from PySide2.QtWidgets import QApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtCore import QUrl
import sys
app = QApplication([])
qml_url = QUrl(sys.argv[0])
view = QQuickView()
However, when I add the next line:
from PySide2.QtWidgets import QApplication
from PySide2.QtQuick import QQuickView
from PySide2.QtCore import QUrl
import sys
app = QApplication([])
qml_url = QUrl(sys.argv[0])
view = QQuickView()
view.setSource(qml_url)
I get the following error:
test.py:1:6: Syntax error
from PySide2.QtWidgets import QApplication
^
I know there are alternative libraries and methods for loading QML in Python, but my question is: why am I getting this syntax error? It's not even on the line I added.
It is not a Python syntax error as noted by @user2357112. The problem is that you are trying to load your Python source code as the QML URL:
qml_url = QUrl(sys.argv[0])
which is simply PySide2.QtCore.QUrl('test.py')
.
The syntax error is raised by the setSource
call, which tells that the first line is not valid QML.
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.