TransWikia.com

Close PyQt Dialog without closing main programme

Stack Overflow Asked by Gilly4297 on February 8, 2021

I am trying to use a PyQt dialog created in Qt Designer to take some user inputs (complexity rating, material type and machine type) before using these input for the reminder of my python programme. The options displayed in the dialog box are read out from a dictionary. My problem is that no matter what I have tried, closing the dialog box, through pressing the submit button, stops the remainder of my programme running, whether I keep the dialog box in the main py programme or run it as a function in a separate file. I am quite sure it is related to the sys.exit(app.exec_()) line, I have also tried using .close and .reject for closing the dialog with the same result. Also I’m aware that the programme is not great and I’m butchering the variable passing out of the function, but if you have any advice for how to get the rest of my programme talking with the dialog box I would be super grateful, I have exhausted the rest of Google on this problem, thanks so much!

import os
import numpy as np

def get_part_info():
    material_ops =[]
    complex_ops = [1,2,3]
    machine_ops = []
    
#---Dictionary containing material options and machine options is read out here, this part works fine ----
    
    mat_choice = 'empty'
    comp_choice = 'empty'
    mach_choice = 'empty'
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(227, 217)
            self.verticalLayout_3 = QtWidgets.QVBoxLayout(Dialog)
            self.verticalLayout_3.setObjectName("verticalLayout_3")
            self.verticalLayout = QtWidgets.QVBoxLayout()
            self.verticalLayout.setObjectName("verticalLayout")
            self.Material = QtWidgets.QComboBox(Dialog)
            self.Material.setObjectName("Material")
            self.verticalLayout.addWidget(self.Material)
            self.Complexity = QtWidgets.QComboBox(Dialog)
            self.Complexity.setObjectName("Complexity")
            self.verticalLayout.addWidget(self.Complexity)
            self.Machine = QtWidgets.QComboBox(Dialog)
            self.Machine.setObjectName("Machine")
            self.verticalLayout.addWidget(self.Machine)
            self.textEdit = QtWidgets.QTextEdit(Dialog)
            self.textEdit.setObjectName("textEdit")
            self.verticalLayout.addWidget(self.textEdit)
            self.verticalLayout_3.addLayout(self.verticalLayout)
            self.Submit = QtWidgets.QPushButton(Dialog)
            self.Submit.setMaximumSize(QtCore.QSize(100, 16777215))
            self.Submit.setObjectName("Submit")
            self.verticalLayout_3.addWidget(self.Submit, 0, QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)

#------Read out from the dictionary is added to the drop down menus here-----

            for i in list(material_ops):
                self.Material.addItem(i)
            for i in list(complex_ops):
                self.Complexity.addItem(str(i))
            for i in list(machine_ops):
                self.Machine.addItem(i)
    
            self.Submit.pressed.connect(self.save)
            self.retranslateUi(Dialog)
            self.Submit.pressed.connect(Dialog.reject)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.Submit.setText(_translate("Dialog", "Submit"))
    
        def save(self):
            global mat_choice, comp_choice, mach_choice
            mat_choice = (self.Material.currentText())
            comp_choice = (self.Complexity.currentText())
            mach_choice = (self.Machine.currentText())
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show() 
        sys.exit(app.exec_())
        return mat_choice, comp_choice, mach_choice, matdict
        
get_part_info()

print('Rest of programme is working') # programme never gets this far

#---The rest of the programme that uses these user chosen options is here and never runs due to the dialog closing stopping the whole programme ------

2 Answers

I solved this by removing the sys.exit(app.exec_()) line and used just app.exec_() instead which successfully runs the input dialog and then the remainder of the programme using the chosen values. I can't pretend to know why it works now but it does in case anyone encounters a similar issue.

Answered by Gilly4297 on February 8, 2021

You cannot close this window and then open up another. You can just hide it after it is no longer applicable.

Answered by George Y on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP