为什么 PyQt5 QFileDialog.getExistingDirectory 看不到 ~/.config/sub



在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)

而且,多亏了@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)

最新更新