简体   繁体   English

关闭 PyQt 对话框而不关闭主程序

[英]Close PyQt Dialog without closing main programme

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.我正在尝试使用在 Qt Designer 中创建的 PyQt 对话框来获取一些用户输入(复杂性评级、材料类型和机器类型),然后再使用这些输入来提醒我的 Python 程序。 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.我的问题是,无论我尝试过什么,关闭对话框,通过按下提交按钮,停止我的程序的其余部分运行,无论是将对话框保留在主 py 程序中还是将其作为单独的函数运行文件。 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.我很确定它与sys.exit(app.exec_())行有关,我也尝试使用 .close 和 .reject 来关闭具有相同结果的对话框。 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 ------

You cannot close this window and then open up another.您不能关闭此窗口然后再打开另一个窗口。 You can just hide it after it is no longer applicable.您可以在它不再适用后将其隐藏。

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.我通过删除sys.exit(app.exec_())行解决了这个问题,只使用sys.exit(app.exec_()) app.exec_()而不是成功运行输入对话框,然后使用所选值运行程序的其余部分。 I can't pretend to know why it works now but it does in case anyone encounters a similar issue.我不能假装知道它为什么现在有效,但如果有人遇到类似的问题,它确实有效。

Set app.setQuitOnLastWindowClosed(False):设置 app.setQuitOnLastWindowClosed(False):

app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)

Credit: Brynn McCullagh信用:布林麦卡拉

It's because the sys.exit(app.exec_()) does 2 things: run the GUI until you close all the GUI and close the program.这是因为sys.exit(app.exec_())做了两件事:运行 GUI,直到关闭所有 GUI 并关闭程序。

exec_() is the method of QApplication that run an event loop, which will close and return a number (0 or something). exec_()是 QApplication 运行事件循环的方法,它将关闭并返回一个数字(0 或其他)。

sys.exit() will quit the program if you pass an integer to it.如果你向它传递一个整数, sys.exit()将退出程序。

So you can use app.exec_() without sys.exit() to run the GUI and then close it without quitting the program.所以,你可以使用app.exec_()sys.exit()来运行GUI,然后关闭不退出程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM