pyqt4 按钮单击处理程序 [英] pyqt4 button click handler

查看:28
本文介绍了pyqt4 按钮单击处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 pyqt4 时遇到问题:这是我正在使用的代码,重点是如果在 pushOk 按钮的事件处理程序中点击我调用 chiedi_numeri() 它不会显示任何形式,而如果我调用 chiedi_numeri() 它说参数 3 og QObjectConnect有一个无效的类型.我该如何解决?谢谢

I have a problem usin pyqt4: here is the code I'm using, the point is that if in the event handler for the pushOk button clicked I call chiedi_numeri() it does not show me any form, while if I call chiedi_numeri() it says that argument 3 og QObjectConnect has an invalid type. how can I solve it? thanks

class Ui_dialog(object):
    def setupUi(self, dialog):
        dialog.setObjectName("dialog")
        dialog.resize(400, 300)
        self.pushOk = QtGui.QPushButton(dialog)
        self.pushOk.setGeometry(QtCore.QRect(200, 150, 75, 23))
        self.pushOk.setObjectName("pushOk")
        self.radioButton = QtGui.QRadioButton(dialog)
        self.radioButton.setEnabled(True)
        self.radioButton.setGeometry(QtCore.QRect(60, 40, 261, 18))
        self.radioButton.setChecked(True)
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtGui.QRadioButton(dialog)
        self.radioButton_2.setGeometry(QtCore.QRect(60, 70, 281, 18))
        self.radioButton_2.setObjectName("radioButton_2")
        self.radioButton_3 = QtGui.QRadioButton(dialog)
        self.radioButton_3.setGeometry(QtCore.QRect(60, 100, 281, 18))
        self.radioButton_3.setObjectName("radioButton_3")
        self.labelRisultato = QtGui.QLabel(dialog)
        self.labelRisultato.setGeometry(QtCore.QRect(70, 200, 261, 51))
        self.labelRisultato.setObjectName("labelRisultato")

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


    def retranslateUi(self, dialog):
        dialog.setWindowTitle(QtGui.QApplication.translate("dialog", "Fibonacci", None, QtGui.QApplication.UnicodeUTF8))
        self.pushOk.setText(QtGui.QApplication.translate("dialog", "Ok", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton.setText(QtGui.QApplication.translate("dialog", "Calcola la serie di Fibonacci", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton_2.setText(QtGui.QApplication.translate("dialog", "Calcola la serie di Fibonacci in un intervallo", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton_3.setText(QtGui.QApplication.translate("dialog", "Calcola l\'ennesimo elemento della serie di Fibonacci", None, QtGui.QApplication.UnicodeUTF8))

def fibonacci():  
    a , b = 0, 1  
    while 1:  
        a, b = b, a + b  
        yield a

def chiedi_numeri(): 
    try:
            max_ = int(raw_input("Inserisci un numero: "))
            if max_ <= 0 :
                print "Il numero", max_, "non e' positivo!"
            else:
                start = 0  
                for i in fibonacci():  
                        if start > max_:
                            break
                        else:
                            start += 1
                            if start == max_:
                                    print  "Il", max_, "numero della sequenza di Fibonacci e'", i
    except ValueError:
            print "Non hai inserito un numero!"


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    dialog = QtGui.QDialog()
    ui = Ui_dialog()
    ui.setupUi(dialog)
    app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri())
    dialog.show()
    sys.exit(app.exec_())

推荐答案

将 Signal 连接到 Python 函数时,传递的是函数对象.换句话说,你省略了尾括号,因为你传递的是函数而不是调用它.尝试更改该行以读取 app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)

When you connect a Signal to a python function, you pass the function object. In other words, you omit the trailing parentheses since you are passing the function not calling it. Try changing that line to read app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)

这篇关于pyqt4 按钮单击处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆