为什么此代码有效:
from PySide.QtCore import *
from PySide.QtGui import *
import sys
from functools import partial
import pyside # my code generated by QT Design
class MainDialog(QMainWindow, pyside.Ui_MainWindow):
def __init__(self, parent=None):
super(MainDialog,self).__init__(parent)
self.setupUi(self)
self.connect(self.Connect_buttom, SIGNAL("clicked()"), partial(self.get_fb_token, "aaaaa","bbbbbb"))
def get_fb_token(self,email,passwd):
print email
print passwd
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()
并打印aaaaa和bbbbb
但如果我改变:
self.connect(self.Connect_buttom, SIGNAL("clicked()"), partial(self.get_fb_token, "aaaaa","bbbbbb"))
到
self.connect(self.Connect_buttom, SIGNAL("clicked()"), partial(self.get_fb_token, self.FB_username.text() ,self.FB_password.text()))
它不打印我在文本框FB_password和FB_username中引入的内容(它不会崩溃,但它不会打印任何内容,如果它不是将两个参数都发送到函数get_fb_token
)??? / p>
**从http://www.blog.pythonlibrary.org/2013/04/10/pyside-connecting-multiple-widgets-to-the-same-slot/获取示例 我正在使用QT和pyside
答案 0 :(得分:2)
定义连接时会创建partial
对象,而不是在触发事件时创建。这意味着在连接时调用FB_username.text()
,因此它将始终打印您在设计器中设置的内容。
要实现您想要的功能,您必须使用一个在调用时检索这些值的函数。 最简单的解决方案是:
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import pyside # my code generated by QT Design
class MainDialog(QMainWindow, pyside.Ui_MainWindow):
def __init__(self, parent=None):
super(MainDialog,self).__init__(parent)
self.setupUi(self)
# use new-style signals & slots!
self.Connect_buttom.clicked.connect(self.get_fb_token)
def get_fb_token(self):
email = self.FB_username.text()
password = self.FB_password.text()
print email
print passwd
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()
如果出于某种原因,您不想修改get_fb_token
,可以像这样使用lambda
:
self.Connect_buttom.clicked.connect(lambda: self.get_fb_token(self.FB_username.text(), self.FB_password.text()))