我刚刚通过pip安装了retext。我必须为此下载图标,但是我意识到它不起作用(菜单上没有图标),除非我在retext文件夹中运行“ retext”。
我试图修复它,但是我的python技能不是很强。
此刻,我强制icon_path具有我想要的路径。
#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'
有人可以启发我这条线是如何工作的吗?
datadirs = [join(d, 'retext') for d in datadirs]
谢谢。
import sys
import markups
import markups.common
from os.path import dirname, exists, join
from PyQt5.QtCore import QByteArray, QLocale, QSettings, QStandardPaths
from PyQt5.QtGui import QFont
app_version = "6.0.1"
settings = QSettings('ReText project', 'ReText')
if not str(settings.fileName()).endswith('.conf'):
# We are on Windows probably
settings = QSettings(QSettings.IniFormat, QSettings.UserScope,
'ReText project', 'ReText')
datadirs = QStandardPaths.standardLocations(QStandardPaths.GenericDataLocation)
datadirs = [join(d, 'retext') for d in datadirs]
if sys.platform == "win32":
# Windows compatibility: Add "PythonXXX\share\" path
datadirs.append(join(dirname(sys.executable), 'share', 'retext'))
if '__file__' in locals():
datadirs = [dirname(dirname(__file__))] + datadirs
#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'
for dir in datadirs:
if exists(join(dir, 'icons')):
icon_path = join(dir, 'icons/')
break
最佳答案
这是os.path.join()
:
os.path.join(path, *paths)
智能地连接一个或多个路径组件。返回值是路径和*paths
的任何成员的连接,在每个非空部分之后,除最后一个以外,os.sep
的成员均带有一个目录分隔符([
),这意味着如果最后一个部分为空的。如果组件是绝对路径,则所有先前的组件都将被丢弃,并且连接将从绝对路径组件继续。
它在这里导入:
from os.path import dirname, exists, join
因此,有关的行:
datadirs = [join(d, 'retext') for d in datadirs]
]
... join(d, 'retext')
是一个列表理解,它构建应用于datadirs
列表中每个目录的datadirs
结果列表。因此,如果
for
包含:['/usr/local/test', '/usr/local/testing', '/usr/local/tester']
然后:
[join(d, 'retext') for d in datadirs]
将产生:
['/usr/local/test/retext', '/usr/local/testing/retext', '/usr/local/tester/retext']
设置问题:
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'
是它在循环中被覆盖,因此,除非找不到正确的路径,否则它将被覆盖。