我使用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)
答案 0 :(得分:1)
在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_())