简体   繁体   中英

Where does this syntax error in my PySide2 script come from?

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.

  • Linux x86_64 (also observed on ARM)
  • Python v3.6.8
  • PySide2 v5.13.0
  • IDE: vim

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.

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