主窗口中的Pyqt对话框仅在按两次OK按钮后关闭

时间:2018-05-30 12:49:56

标签: pyqt

我有一个主窗口,其中包含一些滑块和旋转框,其值来自另一个编辑对话框。所有.ui文件都已由pyuic4转换,并在主程序中使用。编辑对话框由滑块和旋转框组成,带有“确定”和“取消”按钮。 Dialog中的数据在主窗口中收到。

主程序代码

import sys
from PyQt4 import QtCore, QtGui
from Test_MainWindow import Ui_MainWindow
from dialogParameters2 import Ui_DialogParameters2


class AppWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()  # python 3 version
        self.setupUi(self)

        self.action_Shutdown.triggered.connect(lambda: self.close_MyWindow())
        self.action_Edit.triggered.connect(self.dialogParametersOpen)       



    def updateSpeedValue(self):
        scaledValue = float(self.hSliderSpeed.value()) / 10  # type: float
        # self.lblSpeedValue.setText(str(self.hSliderSpeed.value()))
        print(scaledValue, type(scaledValue))
        self.lblSpeedValue.setText(str(scaledValue))


    def dialogParametersOpen(self):
        dialogParameters = CreateParameterDialog()

        # dialogParameters.show()
        if dialogParameters.exec_():
            print("Speed: %s, Rounds: %s, Overlap: %s " % (str(dialogParameters.lblSpeedValue.text()), \
                                                           str(dialogParameters.spinBoxRounds.value()),
                                                           str(dialogParameters.lblOverlapValue.text())))
            self.hSliderSpeed.setValue(dialogParameters.hSliderSpeed.value())
            self.spinBoxRounds.setValue(dialogParameters.spinBoxRounds.value())
            self.hSliderOverlapDeg.setValue(dialogParameters.hSliderOverlap.value())
        print('Dialog Completed with ' + str(dialogParameters.exec_()))

    def close_MyWindow(self):
        print("Window Closed")
        # sys.exit()


class CreateAboutDialog(QtGui.QDialog, Ui_DialogAbout):
    def __init__(self):
        super().__init__()
        # Python2 version
        # super(self.__class__,self).__init__()
        self.setupUi(self)


class CreateParameterDialog(QtGui.QDialog, Ui_DialogParameters2):
    def __init__(self):
        super().__init__()
        # Python2 version
        # super(self.__class__,self).__init__()
        self.setupUi(self)

        sendParametersData = QtCore.pyqtSignal(int,int,int)

        self.pushBtnOK.clicked.connect(self.accept)
        self.pusBtnCancel.clicked.connect(self.reject)

        self.hSliderOverlap.valueChanged.connect(self.lblOverlapValue.setNum)
        # self.hSliderSpeed.valueChanged.connect(self.lblSpeedValue.setNum)

        self.hSliderSpeed.valueChanged.connect(self.updateSpeedData)
        # self.hSliderOverlap.valueChanged.connect(self.updateOverlapData)


    def updateSpeedData(self):
        scaledValue = float(self.hSliderSpeed.value()) / 10  # type: float
        # self.lblSpeedValue.setText(str(self.hSliderSpeed.value()))
        print('value is: ')
        print(scaledValue, type(scaledValue))
        self.lblSpeedValue.setText(str(scaledValue))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = AppWindow()  # type: Instance of the Class
    window.show()
    sys.exit(app.exec_())

现在,在编辑对话框中的所有数据后,当我按下OK按钮一次时,数据将被传输,但对话框未关闭。在第二次按下OK按钮时,对话框关闭。我做错了什么?

0 个答案:

没有答案