[英]Close PyQt Dialog without closing main programme
我正在嘗試使用在 Qt Designer 中創建的 PyQt 對話框來獲取一些用戶輸入(復雜性評級、材料類型和機器類型),然后再使用這些輸入來提醒我的 Python 程序。 對話框中顯示的選項是從字典中讀出的。 我的問題是,無論我嘗試過什么,關閉對話框,通過按下提交按鈕,停止我的程序的其余部分運行,無論是將對話框保留在主 py 程序中還是將其作為單獨的函數運行文件。 我很確定它與sys.exit(app.exec_())
行有關,我也嘗試使用 .close 和 .reject 來關閉具有相同結果的對話框。 此外,我知道該程序不是很好,我正在處理傳遞出函數的變量,但是如果您對如何讓我的程序的其余部分與對話框進行對話有任何建議,我將不勝感激,我已經在這個問題上用盡了谷歌的其余部分,非常感謝!
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 ------
您不能關閉此窗口然后再打開另一個窗口。 您可以在它不再適用后將其隱藏。
我通過刪除sys.exit(app.exec_())
行解決了這個問題,只使用sys.exit(app.exec_())
app.exec_()
而不是成功運行輸入對話框,然后使用所選值運行程序的其余部分。 我不能假裝知道它為什么現在有效,但如果有人遇到類似的問題,它確實有效。
設置 app.setQuitOnLastWindowClosed(False):
app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
信用:布林麥卡拉
這是因為sys.exit(app.exec_())
做了兩件事:運行 GUI,直到關閉所有 GUI 並關閉程序。
exec_()
是 QApplication 運行事件循環的方法,它將關閉並返回一個數字(0 或其他)。
如果你向它傳遞一個整數, sys.exit()
將退出程序。
所以,你可以使用app.exec_()
不sys.exit()
來運行GUI,然后關閉不退出程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.