0

I converted a .ui file of a project built on PyQtDesigner into a file .py. In an external IDE, the code works and the dialog window shows, while on the Python console in Qgis doesn't work. Here is the code.

# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(553, 419)
        ...
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        ...
    

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

I think the problem is the row if __name__ == "__main__": but I don't know how to resolve this. Any Ideas?

0

1 Answer 1

0

When using the Python console, it seems that the __name__ variable is set to "__console__", even if you execute a script.

I guess you're just testing in the console, so you could just change the if to:

if __name__ == "__console__":

(Also small warning; sys.exit(app.exec_()) will close QGIS).

4
  • Thanks! Yes I am testing on console. About that row (sta.exit(app.exec__()), have I to delete It? Commented Jul 17 at 19:57
  • @SalvatoreDiolosà Certainly if you are using the QGIS Python console, sys.exit() always shuts down QGIS it seems. Or you could just comment it out. Doesn't seem appropriate in your script anyway, it's handy if you want to end a script with an exit code. Commented Jul 17 at 20:25
  • It works, but when I run this script for the second time, QGIS closes itself. Commented Jul 18 at 13:54
  • I resolved. I had to delete the row app = QtWidgets.QApplication(sys.argv) Commented Jul 25 at 13:42

Your Answer

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

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