Anekdotin
Anekdotin

Reputation: 1591

PYQT directory dialog opens twice

I have a function which gets the directory by a button. Whenever I want to use the directory thats retrieved with other functions, it makes the open dialog appear again. Any way to work around this. I created another function to call that will hopefully avoid it but still isnt working. Here is what im trying..

def selectFilecsvtoxml(self):


    directory = QtGui.QFileDialog.getExistingDirectory(self, caption="Pick a folder", directory=QtCore.QDir.currentPath())


    print directory + " this si dirrrrectory"
    self.listDirPath.setText(directory)

    for file_name in os.listdir(directory):
        if not file_name.startswith("."):

            print (file_name) +  "   this is selectFilcestoxml"

    return directory


def showDirectory(self):
    showDir = self.selectFilecsvtoxml
    dir = str(showDir)
    print showDir + " this is the files from this class which makes dumbpop"
    print dir + "  this might fix it"
    return dir

Upvotes: 0

Views: 717

Answers (1)

Mel
Mel

Reputation: 6065

You created a function which shows a dialog and returns the directory chosen by the user. The purpose of the function is to ask the user something you don't know, here a path to a folder.
Once you know the directory, there is no need to ask the user again: you shouldn't call this function more than once.

Instead, you should store the value that the user gave you. Here's a simple example:

class myWidget(QtGui.QWidget):
    def __init__(self,parent=None):
        ...
        self.directory=None
        self.button=QtGui.QPushButton("choose a folder")
        self.button.clicked.connect(self.select)

   def select(self):
       self.directory=QtGui.QFileDialog...

   def do_stuff_with_directory(self):
       print(self.directory)

At the beginning, self.directory is None because you don't know what it should be. When the user click the "chose a folder" button, self.directory is set to their choice.
Since it's an attribute of MyWidget, you can use it in any of it's method. You should just check that it is not None before using it.


I saw that you wrote self.listDirPath.setText(directory).
So another way to get the chosen directory in any function would be:

directory=self.listDirPath.text()

Upvotes: 1

Related Questions