Hi Cris,
Welcome to Pyside programming!
I do not know what you intend to do, but based on your example I can think of 2
main options and a 3rd option that is simply a lambda version of the 2nd one.
Here is the example code:
import sys
from PySide import QtCore
from PySide import QtGui
class Form(QtGui.QDialog):
greetings = QtCore.Signal(str)
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent=None)
self.edit = QtGui.QLineEdit("Write my name here")
self.button = QtGui.QPushButton("Show Greetings 1")
self.special_button = QtGui.QPushButton("Show Greetings 2")
self.lambda_button = QtGui.QPushButton("Show Greetings 3")
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.edit)
self.layout.addWidget(self.button)
self.layout.addWidget(self.special_button)
self.layout.addWidget(self.lambda_button)
self.setLayout(self.layout)
# option 1
self.button.clicked.connect(self.greetings)
# option 2
def lambdaGReetings ():
greetings(self)
self.special_button.clicked.connect(lambdaGReetings)
# option 3
self.lambda_button.clicked.connect(lambda: greetings(self))
def greetings (self):
greetings(self)
def greetings(self):
print ("Hello %s" % self.edit.text())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())
Option 1 implies that something has to be done before or after calling the
greetings option inside your class.
If, you just want to call the greetings and nothing else, you can define a
local function as I did in option 2. You can also do this by using the lambda
function, though I personally prefer defining functions.
self.button.clicked.connect(greetings)
=> You are not passing the self argument is passed
self.button.clicked.connect(lambda e: greetings(self))
=> Lambda is expecting a "e" argument, but the button clicked signal
will not give one. For it to send an argument you should explicitlly call the
clicked signal version that cointains a bool argument:
# this modified version will work
self.lambda_button.clicked[bool].connect(lambda e: greetings(self))
self.button.clicked.connect(lambda e, self=self:greetings(self)) =>
=> you do not need to define self, because it is already abailable in
the scope.
I hope this helps and that I was clear.
Best regards,
ASO
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
craf
Sent: jeudi 5 avril 2012 00:14
To: [email protected]
Subject: [PySide] Passing parameter from one class to an external function
Hi Everyone!.
Recently start with Pyside and I would like to know how I can call the
function greetings from within the Form class, passing the self argument.
My Code:---------------------------------------------------
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent=None)
self.edit = QLineEdit("Write my name here")
self.button = QPushButton("Show Greetings")
self.layout = QVBoxLayout()
self.layout.addWidget(self.edit)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(greetings)
def greetings(self):
print ("Hello %s" % self.edit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())
----------------------------------------------------
I try with:
self.button.clicked.connect(greetings)
self.button.clicked.connect(lambda e: greetings(self))
self.button.clicked.connect(lambda e, self=self:greetings(self))
but, nothing.
I appreciate any information
Best Regards
Cris
_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside
_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside