按下保存按钮pyqt5后关闭第二个小部件

时间:2018-03-25 12:29:19

标签: python-3.x pyqt5

我正在编写一个应用程序,主窗口小部件窗口打开第二个窗口小部件窗口,在第二个窗口小部件窗口中,我从用户那里获取一些输入并点击保存按钮,第二个窗口小部件窗口应该将数据保存到xml文件中并且应该关闭但第二个窗口没有关闭。 我尝试了谷歌中的大部分内容,例如sys.exit()它们都没有。

我不想做from PyQt5 import QtCore, QtGui, QtWidgets class Ui_firstWidgetWindow(QtWidgets.QMainWindow): def __init__(self,firstWidgetWindow): super().__init__() self.setupUi(firstWidgetWindow) def setupUi(self, firstWidgetWindow): ### code to create Button ### self.btnOpenNewWidgetWindow.clicked.connect(self.openNewWindow) def openNewWindow(self): self.secondWidgetWindow = QtWidgets.QWidget() self.ui = Ui_secondWidgetWindow() self.ui.setupUi(self.secondWidgetWindow) self.secondWidgetWindow.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) firstWidgetWindow = QtWidgets.QWidget() ui = Ui_firstWidgetWindow(firstWidgetWindow) firstWidgetWindow.show() sys.exit(app.exec_()) ,因为这是关闭完整的应用程序,但只需在单击“保存”按钮后关闭secondWidgetWindow,以便用户可以在第一个窗口小部件窗口中执行另一项工作。

以下是摘录:

FirstWidgetWindow.py

class Ui_secondWidgetWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

    def setupUi(self, secondWidgetWindow):
        ### creating line edit to take input from user
        ### creating save button

        self.btnSave.clicked.connect(self.saveUserInput)

    def saveUserInput(self):
        ## saving user inputs in xml file
        self.close()  ## here i needs to close this window.

secondWidgetWindow.py

convo = [-sum(g[j] * w[i - j] * dz for j in range(0, i)) for i in range(0, len(g))]

1 个答案:

答案 0 :(得分:1)

Close second widget after pressing save button:

self.secondWidgetWindow.hide()

Try it:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_secondWidgetWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.secondWidgetWindow = None

    def setupUi(self, secondWidgetWindow):
        self.secondWidgetWindow = secondWidgetWindow
        ### creating line edit to take input from user
        self.line_edit = QtWidgets.QLineEdit(secondWidgetWindow)
        self.line_edit.setGeometry(20, 20, 300, 20)

        ### creating save button
        self.btnSave = QtWidgets.QPushButton('save', secondWidgetWindow)
        self.btnSave.setGeometry(50, 50, 100, 50)
        self.btnSave.clicked.connect(self.saveUserInput)

    def saveUserInput(self):
        ## saving user inputs in xml file
        #self.close()  ## here i needs to close this window.
        self.secondWidgetWindow.hide()
        QtWidgets.QMessageBox.information(self, "SAVE",
                                        "saving user inputs in xml file")


class Ui_firstWidgetWindow(QtWidgets.QMainWindow):
    def __init__(self,firstWidgetWindow):
        super().__init__()

        self.setupUi(firstWidgetWindow)

    def setupUi(self, firstWidgetWindow):
        ### code to create Button ###
        self.btnOpenNewWidgetWindow = QtWidgets.QPushButton('OpenNewWidgetWindow', firstWidgetWindow)
        self.btnOpenNewWidgetWindow.setGeometry(50, 100, 300, 50)
        self.btnOpenNewWidgetWindow.clicked.connect(self.openNewWindow)

    def openNewWindow(self):
        self.secondWidgetWindow = QtWidgets.QWidget()
        self.ui = Ui_secondWidgetWindow()
        self.ui.setupUi(self.secondWidgetWindow)
        self.secondWidgetWindow.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    firstWidgetWindow = QtWidgets.QWidget()
    ui = Ui_firstWidgetWindow(firstWidgetWindow)
    firstWidgetWindow.setGeometry(700, 250, 400, 200)
    firstWidgetWindow.show()
    sys.exit(app.exec_())

enter image description here