在Pyside中,如何在单击关闭按钮时处理近距离通话?

时间:2015-10-16 22:05:20

标签: pyside

我必须处理的事情似乎不起作用。运行它时得到的错误是:

  

错误:AttributeError:文件第82行:' Ui_Dialog'对象没有属性'关闭'

from PySide import QtCore, QtGui, QtUiTools
import maya.cmds as cmds

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 20, 131, 16))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))

        ............
        ................
        ..................
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.swapRefBtn.clicked.connect(self.swapRefBtn_clicked)
        self.closeBtn.clicked.connect(self.close())       <---- THIS HERE WON'T WORK        

    def retranslateUi(self, Dialog):
        ............
        ................
        ..................


    def swapRefBtn_clicked(self):
       pass                 

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)       
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    app.exec_()

1 个答案:

答案 0 :(得分:0)

也许您想将关闭按钮连接到 QDialog.accept QDialog.reject 广告位。

至于如何处理它们:

示例

import sys
import PyQt4.QtGui as qg

class Dialog(qg.QDialog):

    def __init__(self):
        super().__init__()
        self.setup_widgets()

    def setup_widgets(self):
        self.ok = qg.QPushButton('OK')
        self.ok.clicked.connect(self.accept)

        self.cancel = qg.QPushButton('Cancel')
        self.cancel.clicked.connect(self.reject)

        layout = qg.QHBoxLayout()
        layout.addWidget(self.ok)
        layout.addWidget(self.cancel)
        self.setLayout(layout)

    def accept(self):
        # Your logic here
        super().accept()

    def reject(self):
        response = qg.QMessageBox.warning(self, 'Warning', 'Cancel?',
                                          qg.QMessageBox.Yes | qg.QMessageBox.No)
        if response == qg.QMessageBox.Yes:
            super().reject()

if __name__ == '__main__':
    app = qg.QApplication(sys.argv)
    dialog = Dialog()
    dialog.show()
    sys.exit(app.exec_())

另见: QDialog.done()