PyQt: QFileDialog.getExistingDirectory using a default directory, user independant

后端 未结 2 1708
执笔经年
执笔经年 2021-02-14 13:56

When using the QFileDialog.getExistingDirectory I\'ve found the way to specify the default path to a directory. I wanted to use a default directory somewhere in my

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 14:34

    You can get the user's home directory by using os.path.expanduser

    >>> from os.path import expanduser
    >>> expanduser("~")
    /home/user_name
    

    This works on Windows and Linux.

    Your code block will look like this then

    my_dir = QtGui.QFileDialog.getExistingDirectory(
        self,
        "Open a folder",
        expanduser("~"),
        QtGui.QFileDialog.ShowDirsOnly
    )
    

提交回复
热议问题