一旦程序运行,就会点击PyQt Button

时间:2016-06-30 04:34:31

标签: python qt python-3.x pyqt pyqt4

使用Qt Designer,我创建了一个简单的对话框。我添加了一个名为self.pushButton的提交按钮。我一直有一个问题,当我运行程序时,按钮会自动点击。我正在将程序打印到shell,"按钮点击"一旦程序运行就会出现。

from PyQt4 import QtCore, QtGui

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(880, 479)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(240, 10, 351, 81))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(25)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(60, 50, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(60, 100, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_3.setFont(font)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.comboBox = QtGui.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(340, 260, 161, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(12)
        self.comboBox.setFont(font)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(714, 412, 121, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(14)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(60, 160, 741, 101))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Rockwell Nova Light"))
        font.setPointSize(15)
        self.label_4.setFont(font)
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName(_fromUtf8("label_4"))

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

        self.home()
        self.click()

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "Welcome to PURA!", None))
        self.label_2.setText(_translate("Dialog", "This app is your revision partner, helping you learn throughout the day.", None))
        self.label_3.setText(_translate("Dialog", "To begin, first add a question below", None))
        self.comboBox.setItemText(0, _translate("Dialog", "Multiple Choice", None))
        self.comboBox.setItemText(1, _translate("Dialog", "True or False", None))
        self.comboBox.setItemText(2, _translate("Dialog", "Written Question", None))
        self.pushButton.setText(_translate("Dialog", "Submit", None))
        self.label_4.setText(_translate("Dialog", "Select the type of question you would like to make", None))

    def home(self):

        self.pushButton.clicked.connect(self.click)

    def click(self):
        print("button clicked")

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

1 个答案:

答案 0 :(得分:0)

您可以在" setupUi"(第75行)功能中调用您的点击功能,该功能在开始时调用以设置您的ui。从那里删除self.click()