为什么PyQt5 QFileDialog.getExistingDirectory无法看到〜/ .config /子目录?

时间:2018-06-01 03:43:21

标签: linux python-3.x pyqt5

在Python2.7和PySide下,我能够指向〜/ .config /的子目录。但是,当我转移到Python3和PyQt5时,我可以打开〜/ .config /但不能打开它的子​​目录...(所有目录都有drwxr-xr-x权限,没有特殊的聊天内容或ACL发生的事情。 )

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Arch Linux (up-to-date)
# Python 3.6.5
# python-pyqt5 5.10.1-3

import os
import sys
from   PyQt5.QtCore    import *
from   PyQt5.QtGui     import *
from   PyQt5.QtWidgets import *

app = QApplication(sys.argv)

# Succeeds. (Lists three files in the autostart directory.)
wd = os.path.expanduser("~/.config/autostart")
os.system("ls {0}".format(wd))

# Fails. Opens to ~/
x = QFileDialog.getExistingDirectory(caption="Choose presets...", directory=wd)

wd = os.path.expanduser("~/.config")

# Succeeds. Opens at ~/.config/
x = QFileDialog.getExistingDirectory(caption="Choose presets...", directory=wd)

# Succeeds. Opens at ~/Documents/Volunteer
wd = os.path.expanduser("~/Documents/Volunteer")
x = QFileDialog.getExistingDirectory(None, "Choose presets...", wd)

1 个答案:

答案 0 :(得分:1)

而且,感谢@ekhumoro我们有一个胜利者!告诉QFileDialog不使用本机对话框就可以了。具体做法是:

#!/usr/bin/evn python3
# -*- coding: utf-8 -*-

# Arch Linux (up-to-date)
# Python 3.6.5
# python-pyqt5 5.10.1-3

import os
import sys
from   PyQt5.QtCore    import *
from   PyQt5.QtGui     import *
from   PyQt5.QtWidgets import *

app = QApplication(sys.argv)

# Succeeds. (Lists three files in the autostart directory.)
wd = os.path.expanduser("~/.config/autostart")
os.system("ls {0}".format(wd))

# SUCCEEDS (where it previously failed)
x = QFileDialog.getExistingDirectory(caption="Choose presets...", directory=wd,
                                     options=QFileDialog.DontUseNativeDialog)