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

后端 未结 2 1704
执笔经年
执笔经年 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:12

    You can also get home folder for any user by obtaining the environment variable "HOME" through os.getenv(varname).

    >>> import os
    >>> os.getenv("HOME")
    '/home/my_user_name'
    

    Your code could look like this:

    import os
    home = os.getenv("HOME")
    
    my_dir = QtGui.QFileDialog.getExistingDirectory(
        self,
        "Open a folder",
        home,
        QtGui.QFileDialog.ShowDirsOnly
        )
    
    0 讨论(0)
  • 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
    )
    
    0 讨论(0)
提交回复
热议问题