Qt interface closes

Hi all,

I am implementing a very simple interface in Qt. The ui code is in a file called “myUI.py” and I want to call it from my “script.py” (call to run_gui). It generates the window but closes it inmediately. How can I fix that?

Running the code run_gui from terminal works.

The UI looks like:
imagen

The code of myUI.py is:

import sys
from qt import QApplication, QMainWindow, QTextBrowser, QLabel, QPushButton, QWidget, QCheckBox, \
    QVBoxLayout, QLineEdit, QApplication
from . import script

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(714, 294)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setGeometry(530, 180, 141, 51)
        font = self.pushButton.font
        font.setPointSize(13)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(script.readUserInputFromGUI)
        self.pushButton.toggle()
        
        self.checkBox = QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(30, 180, 391, 51)
        font = self.checkBox.font
        font.setPointSize(13)
        self.checkBox.setFont(font)
        self.checkBox.setObjectName("checkBox")
        self.checkBox.setChecked(True)
        
        self.widget = QWidget(self.centralwidget)
        self.widget.setGeometry(30, 45, 641, 121)
        self.widget.setObjectName("widget")
        
        self.gridLayout = QVBoxLayout(self.widget)
        
        self.label = QLabel(self.widget)
        font = self.label.font
        font.setPointSize(13)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label)
        
        self.lineEdit = QLineEdit(self.widget)
        font = self.lineEdit.font
        font.setPointSize(12)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setFixedWidth(650)
        self.lineEdit.setFixedHeight(20)
        self.gridLayout.addWidget(self.lineEdit)
        
        self.label_2 = QLabel(self.widget)
        font = self.label_2.font
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2)
        
        self.lineEdit_2 = QLineEdit(self.widget)
        font = self.lineEdit_2.font
        font.setPointSize(12)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_2.setFixedWidth(650)
        self.lineEdit_2.setFixedHeight(20)
        self.gridLayout.addWidget(self.lineEdit_2)
        
        self.label_3 = QLabel(self.widget)
        font = self.label_3.font
        font.setPointSize(13)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3)
        
        self.lineEdit_3 = QLineEdit(self.widget)
        font = self.lineEdit_3.font
        font.setPointSize(12)
        self.lineEdit_3.setFont(font)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_3.setFixedWidth(200)
        self.lineEdit_3.setFixedHeight(20)
        self.gridLayout.addWidget(self.lineEdit_3)
        
        MainWindow.setCentralWidget(self.centralwidget)
        
        self.retranslateUi(MainWindow)
        
        
    def retranslateUi(self, MainWindow):
        _translate = QApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Continue"))
        self.checkBox.setText(_translate("MainWindow", "Select if you want to reslice the volume"))
        self.label.setText(_translate("MainWindow", "Path to patients folder"))
        self.label_2.setText(_translate("MainWindow", "Path to save results"))
        self.label_3.setText(_translate("MainWindow", "Patient ID"))

def run_gui():
    #app = QApplication
    form = QMainWindow()
    window = Ui_MainWindow()
    window.setupUi(form)
    form.show()
    # sys.exit(app.exec_())

I tried to use QApplication but I had the same problem.

Thanks,

How are you running your script?

The code is part of a module I’m working on. The idea is to do import moduleName on python console and then I want the interface to open.

Module stucture is:
init.py —calls—> script.py —calls—> ui.py to open ui

Probably your UI variables are going out of scope so they are deleted automatically.

Try first with a simple script that just shows a button and try different ways to launch the script and store your variables.

Defining the mainWindow variable as “global” works.

Thanks