# Qt interface closes

**URL:** <https://discourse.slicer.org/t/qt-interface-closes/32158>\
**Category:** Support\
**Created:** [October 11, 2023, 2:48pm UTC](https://discourse.slicer.org/t/qt-interface-closes/32158 "2023-10-11T14:48:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 11, 2023, 2:48pm UTC](https://discourse.slicer.org/t/qt-interface-closes/32158/1 "2023-10-11T14:48:38Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/d/6d0bbeb1b2b7047ffcec13a0d25f61912b33cf14.png)

The code of `myUI.py` is:

```auto
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,

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 11, 2023, 4:50pm UTC](https://discourse.slicer.org/t/qt-interface-closes/32158/2 "2023-10-11T16:50:57Z")

</div>

> [@bserrano](#):
>
> I want to call it from my “script.py” (call to `run_gui`).

How are you running your script?

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 13, 2023, 12:39pm UTC](https://discourse.slicer.org/t/qt-interface-closes/32158/3 "2023-10-13T12:39:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 13, 2023, 1:36pm UTC](https://discourse.slicer.org/t/qt-interface-closes/32158/4 "2023-10-13T13:36:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 18, 2023, 11:21am UTC](https://discourse.slicer.org/t/qt-interface-closes/32158/5 "2023-10-18T11:21:20Z")

</div>

Defining the mainWindow variable as “global” works.

Thanks
