PyQt:QFileDialog.getExistingDirectory 使用默认目录,用户独立 [英] PyQt: QFileDialog.getExistingDirectory using a default directory, user independant

查看:428
本文介绍了PyQt:QFileDialog.getExistingDirectory 使用默认目录,用户独立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 QFileDialog.getExistingDirectory 时,我找到了指定目录默认路径的方法.我想在我的 Linux (Mint) 机器上的(用户)主文件夹中的某处使用默认目录.我用过:

my_dir = QtGui.QFileDialog.getExistingDirectory(自己,"打开文件夹","/home/my_user_name/",QtGui.QFileDialog.ShowDirsOnly)

效果很好.下一个级别"现在是能够为每个用户执行此操作.我尝试了标准的 Linux 方式并将 /home/my_user_name/ 更改为 ~/.那没有用.它导致工作目录而不是用户的目录.

从终端调用我的应用程序时,我应该使用更多系统参数吗?还是我缺少 PyQt 函数?

解决方案

您可以使用 os.path.expanduser

<预><代码>>>>从 os.path 导入 expanduser>>>展开用户(~")/家/用户名

这适用于 Windows 和 Linux.

你的代码块将如下所示

my_dir = QtGui.QFileDialog.getExistingDirectory(自己,"打开文件夹",展开用户(~"),QtGui.QFileDialog.ShowDirsOnly)

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 (user) home folder on my Linux (Mint) machine. I used:

my_dir = QtGui.QFileDialog.getExistingDirectory(
    self,
    "Open a folder",
    "/home/my_user_name/",
    QtGui.QFileDialog.ShowDirsOnly
    )

Which worked beautifully. The next 'level' is now to be able to do this for every user. I tried the standard Linux way and change /home/my_user_name/ to ~/. That did not work. It resulted in the working directory instead of the user's dir.

Should I use more system arguments when calling my app from the terminal? Or am I missing a PyQt function?

解决方案

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
)

这篇关于PyQt:QFileDialog.getExistingDirectory 使用默认目录,用户独立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆