pyqt4 button click handler
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.radio开发者_开发知识库Button.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_())
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)
One thing you're doing wrong is in this line:
app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri())
You want this function to run when the button is clicked, not when the connection is made. To do that, remove the parentheses after chiedi_numeri
:
app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)
That should then wire up your button correctly.
However, I'm not sure what you mean by the following:
I call chiedi_numeri() it does not show me any form
What do you mean by 'form'? Are you expecting another window to pop up and ask for a number? At the moment, your program asks for a number in the terminal/shell/console window you started the application from.
from PyQt4 import QtCore, QtGui from PyQt4.QtCore import * from PyQt4.QtGui import *
try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s
class Ui_Dialog(object): def setupUi(self, dialog): dialog.setObjectName(_fromUtf8("dialog")) dialog.resize(600, 400) 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.lineEdit = QtGui.QLineEdit(dialog)
self.lineEdit.setGeometry(QtCore.QRect(240, 39, 150, 18))
self.lineEdit.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 248);"))
self.lineEdit.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.textEdit = QtGui.QTextEdit(dialog)
self.textEdit.setGeometry(QtCore.QRect(10, 220, 250, 150))
self.textEdit.setStyleSheet(_fromUtf8("background-color: rgb(220, 220, 220);font: 17pt \"Adobe Courier\";"))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
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))
class StaxQT4(QtGui.QDialog): def init(self):
QtGui.QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.connect(self.ui.pushOk, QtCore.SIGNAL("clicked()"), self.chiedi_numeri)
def fibonacci(self):
a , b = 0, 1
while 1:
a, b = b, a + b
yield a
def chiedi_numeri(self):
try:
max_ = int(self.ui.lineEdit.text())
if max_ <= 0 :
self.ui.lineEdit.setText( "Il numero %s non e' positivo!" % str(max_) )
else:
start = 0
for i in self.fibonacci():
if start > max_:
break
else:
start += 1
if start == max_:
self.ui.textEdit.setText("Il %s numero della sequenza di Fibonacci e'" % str(max_))
self.ui.textEdit.append(str(i))
except ValueError:
self.ui.lineEdit.settext("Non hai inserito un numero!")
if name == "main": import sys app = QtGui.QApplication(sys.argv)
myapp = StaxQT4()
myapp.show()
sys.exit(app.exec_())
精彩评论