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

后端 未结 2 1711
执笔经年
执笔经年 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
        )
    

提交回复
热议问题