在不关闭主程序的情况下关闭 PyQt 对话框
Close PyQt Dialog without closing main programme
我正在尝试使用在 Qt Designer 中创建的 PyQt 对话框来获取一些用户输入(复杂性评级、material 类型和机器类型),然后再将这些输入用于提醒我的 python程序。对话框中显示的选项是从字典中读出的。我的问题是,无论我尝试了什么,关闭对话框,通过按下提交按钮,停止我程序的其余部分 运行ning,无论我是将对话框保留在主 py 程序中还是 运行 它作为一个单独文件中的函数。我很确定它与 sys.exit(app.exec_())
行有关,我也尝试使用 .close 和 .reject 关闭对话框,结果相同。我也知道这个程序不是很好,我正在破坏从函数中传递出来的变量,但是如果你有任何关于如何让我的程序的其余部分与对话框对话的建议,我将非常感激,我已经用尽了 Google 剩下的时间来解决这个问题,非常感谢!
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 ------
您不能关闭这个 window 然后再打开另一个。
您可以在它不再适用后将其隐藏。
我通过删除 sys.exit(app.exec_())
行并只使用 app.exec_()
来解决这个问题,它成功运行了输入对话框,然后使用所选值成功运行了程序的其余部分。我不能假装知道为什么它现在可以工作,但它确实可以,以防万一有人遇到类似问题。
设置app.setQuitOnLastWindowClosed(假):
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
来源:
这是因为 sys.exit(app.exec_())
做了两件事:运行 GUI,直到您关闭所有 GUI 并关闭程序。
exec_()
是 QApplication 的方法,运行 一个事件循环,它将关闭并且 return 一个数字(0 或其他)。
sys.exit()
如果你传递一个整数给它会退出程序。
因此您可以使用 app.exec_()
而无需 sys.exit()
到 运行 GUI,然后在不退出程序的情况下关闭它。
我正在尝试使用在 Qt Designer 中创建的 PyQt 对话框来获取一些用户输入(复杂性评级、material 类型和机器类型),然后再将这些输入用于提醒我的 python程序。对话框中显示的选项是从字典中读出的。我的问题是,无论我尝试了什么,关闭对话框,通过按下提交按钮,停止我程序的其余部分 运行ning,无论我是将对话框保留在主 py 程序中还是 运行 它作为一个单独文件中的函数。我很确定它与 sys.exit(app.exec_())
行有关,我也尝试使用 .close 和 .reject 关闭对话框,结果相同。我也知道这个程序不是很好,我正在破坏从函数中传递出来的变量,但是如果你有任何关于如何让我的程序的其余部分与对话框对话的建议,我将非常感激,我已经用尽了 Google 剩下的时间来解决这个问题,非常感谢!
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 ------
您不能关闭这个 window 然后再打开另一个。 您可以在它不再适用后将其隐藏。
我通过删除 sys.exit(app.exec_())
行并只使用 app.exec_()
来解决这个问题,它成功运行了输入对话框,然后使用所选值成功运行了程序的其余部分。我不能假装知道为什么它现在可以工作,但它确实可以,以防万一有人遇到类似问题。
设置app.setQuitOnLastWindowClosed(假):
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
来源:
这是因为 sys.exit(app.exec_())
做了两件事:运行 GUI,直到您关闭所有 GUI 并关闭程序。
exec_()
是 QApplication 的方法,运行 一个事件循环,它将关闭并且 return 一个数字(0 或其他)。
sys.exit()
如果你传递一个整数给它会退出程序。
因此您可以使用 app.exec_()
而无需 sys.exit()
到 运行 GUI,然后在不退出程序的情况下关闭它。