pyqt4按钮点击处理程序

发布于 2024-11-09 22:28:35 字数 3116 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-11-16 22:28:35

当您将 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)

甜妞爱困 2024-11-16 22:28:35

您做错的一件事是在这一行中:

    app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri())

您希望此函数在单击按钮时运行,而不是在建立连接时运行。为此,请删除 chiedi_numeri 之后的括号:

    app.connect(ui.pushOk, Qt.SIGNAL("clicked()"), chiedi_numeri)

这样就可以正确连接您的按钮。

但是,我不确定您所说的以下内容是什么意思:

我调用 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.

黎歌 2024-11-16 22:28:35

从 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")

    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(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
而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 == "ma​​in":
导入系统
应用程序 = QtGui.QApplication(sys.argv)

myapp = StaxQT4()
myapp.show()

sys.exit(app.exec_())

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_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文