pyqt4按钮点击处理程序
我在使用 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_())
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_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您将 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)
您做错的一件事是在这一行中:
您希望此函数在单击按钮时运行,而不是在建立连接时运行。为此,请删除
chiedi_numeri
之后的括号:这样就可以正确连接您的按钮。
但是,我不确定您所说的以下内容是什么意思:
“形式”是什么意思?您是否希望弹出另一个窗口并询问号码?目前,您的程序会在您启动应用程序的终端/外壳/控制台窗口中要求输入一个号码。
One thing you're doing wrong is in this line:
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
:That should then wire up your button correctly.
However, I'm not sure what you mean by the following:
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.
从 PyQt4 导入 QtCore、QtGui
从 PyQt4.QtCore 导入 *
从 PyQt4.QtGui 导入 *
尝试:
_fromUtf8 = QtCore.QString.fromUtf8
除了属性错误:
_fromUtf8 = lambda s: s
类 Ui_Dialog(object):
def setupUi(自身,对话框):
dialog.setObjectName(_fromUtf8(“对话框”))
对话框.调整大小(600, 400)
self.pushOk = QtGui.QPushButton(对话框)
self.pushOk.setGeometry(QtCore.QRect(200, 150, 75, 23))
self.pushOk.setObjectName("pushOk")
类 StaxQT4(QtGui.QDialog):
def init(self):
def fibonacci(self):
a , b = 0, 1
而1:
a、b = b、a + b
产生一个
def chiedi_numeri(self):
尝试:
max_ = int(self.ui.lineEdit.text())
如果 max_ <= 0 :
self.ui.lineEdit.setText( "Il numero %s non e' positivo!" % str(max_) )
别的:
开始 = 0
对于 self.fibonacci() 中的 i:
如果开始>最大限度_:
休息
别的:
开始+=1
如果开始== max_:
self.ui.textEdit.setText("Il %s 斐波那契数列'" % str(max_))
self.ui.textEdit.append(str(i))
除了值错误:
self.ui.lineEdit.settext("Non hai inserito un numero!")
if name == "main":
导入系统
应用程序 = QtGui.QApplication(sys.argv)
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")
class StaxQT4(QtGui.QDialog):
def init(self):
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)