Registered User
Registered User

Reputation: 1972

Pyside: Pass a var generated in a function to another function

The following functions are both activated by a clicked signal of a separate QPushButton. I want to give the user the possibility to choose a .pdf file, do some program specific edits and save it to a user defined location.

How can I make so that the path to the .pdf is passed to another function?


def select(self):
    dir = "."
    fileObjOpen = QFileDialog.getOpenFileName(self, "Select a .pdf file", dir=dir, filter="PDF Files (*.pdf)")
    fileObjOpenName = fileObjOpen[0]

    if (len(fileObjOpenName) > 1):
        path_to_pdf = fileObjOpenName

def save(self):
    dir = "."
    fileObjSave = QFileDialog.getSaveFileName(self, "Where to save the new pdf file", dir=dir, filter="PDF Files (*.pdf)")
    fileNameSave = fileObjSave[0]

    if (len(fileNameSave) > 1):
        path_to_pdf = path_to_pdf

Upvotes: 0

Views: 120

Answers (2)

mgilson
mgilson

Reputation: 309821

I don't know anything about pyside, but it seems like you're using a class since self is the first argument to each of your functions. If that's the case, you can just set path_to_pdf as an attribute on the class instance.

e.g.

def select(self):
    #snip ...
    if len(fileObjOpenName) > 1:
       self.path_to_pdf = fileObjOpenName

def save(self):
    #snip ...
    if len(fileNameSave) > 1:
       path_to_pdf = self.path_to_pdf

If I've horribly misinterpreted this, let me know and I'll delete my answer.

Upvotes: 2

Pierre GM
Pierre GM

Reputation: 20329

Make sure that your functions return something...:

def select(self):
    ...
    (filename, fileselector) = QFileDialog.getOpenFileName(...)
    if filename:
        return filename
    return

By default, select, like any method or function in Python, will return None, so the last return is superfluous (but I like showing where it actually ends).

Now, you would call your other_function as other_function(YourWidget.select()).

You could also store the output of .select() in an attribute of your QWidget

Upvotes: 1

Related Questions