2

This code Using PyQt5 extracted from http://www.tutorialspoint.com does not work when I add it in a script in the console of QGIS 3.16. Can you enlighten me on this subject?

import sys
from PyQt5 import QtGui

def window():
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    b = QtGui.QLabel(w)
    b.setText("Hello World!")
    w.setGeometry(100,100,200,50)
    b.move(50,20)
    w.setWindowTitle("PyQt")
    w.show()
    sys.exit(app.exec_())
    
    if __name__ == '__main__':
       window()

1 Answer 1

5

I think the original code have a different indentation :

import sys
from PyQt5 import QtWidgets

def window():
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    b = QtWidgets.QLabel(w)
    b.setText("Hello World!")
    w.setGeometry(100,100,200,50)
    b.move(50,20)
    w.setWindowTitle("PyQt")
    w.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    window()

It's a code to create a widget with a label Hello World! inside a QApplication app loop.

But, when you launch QGIS, it's already a Qt application with its own loop. So, the working code will be :

import sys
from PyQt5 import QtWidgets

def window():
    w = QtWidgets.QWidget()
    b = QtWidgets.QLabel(w)
    b.setText("Hello World!")
    w.setGeometry(100,100,200,50)
    b.move(50,20)
    w.setWindowTitle("PyQt")
    return w


w = window()
w.show()
3
  • Thinks, It's OK, but after i shall like to install in this box coordinates (X,Y) and an EPSG etc... to create a points or lines. Where can I found examples dealing with this problem. Have you an idea about this.
    – M. Labriki
    Feb 4, 2021 at 16:32
  • I suggest you to start with the official documentation : docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/plugins/… and to install Plugin Builder QGIS plugin to have a plugin with a simple dialog you can modify and improve. After, you have to know Python and how to develop with PyQt. Feb 4, 2021 at 16:41
  • Im going to try this. Think you for your help and for your advice.
    – M. Labriki
    Feb 5, 2021 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.