我在pyqt4中遇到了问题: 这里是我正在使用的代码,重点是如果在pushOk按钮的事件处理程序中单击我调用chiedi_numeri()它不会向我显示任何形式,而如果我调用chiedi_numeri()它会说参数3 og QObjectConnect有一个无效的类型。我怎么解决呢? 感谢
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_())
答案 0 :(得分:1)
将Signal连接到python函数时,传递函数对象。换句话说,您省略了尾随括号,因为您传递的函数没有调用它。尝试更改该行以阅读app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)
答案 1 :(得分:0)
你做错了一件事是在这一行:
app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri())
您希望在单击按钮时运行此功能,而不是在建立连接时运行。为此,请在chiedi_numeri
之后删除括号:
app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)
然后应正确连接按钮。
但是,我不确定你的意思如下:
我打电话给chiedi_numeri()它没有给我任何表格
'形式'是什么意思?您是否期望弹出另一个窗口并询问一个号码?目前,您的程序会在您启动应用程序的终端/ shell /控制台窗口中询问一个数字。
答案 2 :(得分:0)
从PyQt4导入QtCore,QtGui 来自PyQt4.QtCore import * 来自PyQt4.QtGui import *
尝试: _fromUtf8 = QtCore.QString.fromUtf8 除了AttributeError: _fromUtf8 = lambda s:s
类Ui_Dialog(对象): def setupUi(self,dialog): dialog.setObjectName(_fromUtf8( “对话”)) dialog.resize(600,400) self.pushOk = QtGui.QPushButton(对话框) 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))
类StaxQT4(QtGui.QDialog): def init (自我):
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
而1:
a,b = b,a + b
屈服
def chiedi_numeri(个体经营):
尝试:
max_ = int(self.ui.lineEdit.text())
如果max_&lt; = 0:
self.ui.lineEdit.setText(“Il numero%s non e'posititivo!”%str(max_))
其他:
start = 0
对于我在self.fibonacci():
如果开始&gt; MAX_:
打破
其他:
开始+ = 1
如果start == max_:
self.ui.textEdit.setText(“Il%s numero della sequenza di Fibonacci e'”%str(max_))
self.ui.textEdit.append(STR(i))的
除了ValueError:
self.ui.lineEdit.settext(“Non hai inserito un numero!”)
如果名称 ==“主要”: 导入系统 app = QtGui.QApplication(sys.argv)
myapp = StaxQT4()
myapp.show()
sys.exit(app.exec_())