通过单击按钮将两个参数发送到函数

To send two arguments to a function with the click of a button

我使用 QT Designer 有两个 QLineEdit 来接收用户的输入。用户输入值后,单击 Enter 按钮时,我需要按钮将值传递给 disk_angles 函数。

如何通过按下按钮通过信号将两个字符串传递给一个函数? 这是我的代码

class Maindialog(QMainWindow,diskgui.Ui_MainWindow):

    pass_arguments = SIGNAL((str,),(str,))

    def __init__(self,parent = None):

        super(Maindialog,self).__init__(parent)
        self.setupUi(self)

        self.connect(self.Home,SIGNAL("clicked()"),self.home_commands)
        self.connect(self.AutoFocus,SIGNAL("clicked()"),self.auto_focus)

        self.Enter.clicked.connect(self.entervalues)
        self.connect(self,SIGNAL("pass arguments"),self.Criterion_disk_angles)

    def entervalues(self):
        if self.RotationEdit.text() != "" and self.TiltEdit.text() != "":
        self.RotationEdit = str(self.RotationEdit.text())
        self.TiltEdit = str(self.TiltEdit.text())
        self.pass_arguments.emit(self.RotationEdit,self.TiltEdit)

    def disk_angles(self,rotation_angle, tilt_angle):

我尝试将元组作为信号的输入传递 pass_arguments = SIGNAL((str,),(str,)) 但我收到错误

         pass_arguments = SIGNAL((str,),(str,))
         TypeError: SIGNAL() takes exactly one argument (2 given)

在PyQt5中推荐使用new style,另外你发送2个元组的地方一个,这里我展示一个正确实现的例子。

import sys

from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import QApplication, QPushButton


class Widget(QObject):
    sig = pyqtSignal((str, str))

    def __init__(self, parent=None):
        super(Widget, self).__init__(parent=parent)
        self.sig.connect(self.printer)

    def click(self):
        self.sig.emit("hello", "bye")

    def printer(self, text1, text2):
        print(text1, text2)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QPushButton()
    w1 = Widget()
    w.clicked.connect(w1.click)
    w.show()
    sys.exit(app.exec_())