【问题标题】:Get Notepad++ to use relative path to image获取 Notepad++ 以使用图像的相对路径
【发布时间】:2018-12-29 11:56:36
【问题描述】:

我可以使用 F5 从 Notepad++ 打开任何 python 文件,除非它引用具有相对路径的图像。必须有一种方法可以让 Notepad++ F5 的工作方式与在 Windows 资源管理器中双击文件名或右键单击相同的文件名以“使用 IDLE 进行编辑”相同。如何让 Notepad++ 打开一个文件,而不用硬编码到我的 .py 文件中,因为它存在于我的计算机上?我的应用程序的最终用户不需要我计算机上的路径。我找不到像我这样的初学者如何修复 NPP 以正确执行此操作的说明。我在 Windows 7 上使用 NPP v7.4.2 32 位。我试过 NPP 论坛,但它的搜索引擎和谷歌都没有找到答案。

只有当我硬编码完整路径时,F5 才能正确打开文件,如顶部未注释的代码行所示。

我以为我找到了答案 here,但是当我尝试时看到下面的第二条错误消息

img4 = tk.PhotoImage(file=os.path.abspath("joe.gif") , master=root)

显然 NPP 正在强制我使用绝对路径。

感谢您的帮助。

import tkinter as tk

root = tk.Tk()

# img4 = tk.PhotoImage(file="joe.gif", master=root)
img4 = tk.PhotoImage(file="c:/tkinter_code/joe.gif" , master=root)
logoimage = tk.Label(root, image=img4)
logoimage.image = img4
logoimage.grid()

root.mainloop()


Traceback (most recent call last):
File "C:\tkinter_code\how_to_get_npp_to_display_images_with_relative_path_tkinter.py", line 9, in <module>     img4 = tk.PhotoImage(file="joe.gif", master=root) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3393, in __init__Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__ init__.py", line 3349, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "joe.gif": no such file or directory

Traceback (most recent call last):
File "C:\tkinter_code\how_to_get_npp_to_display_images_with_relative_path_tkinter.py", line 14, in <module>     img4 = tk.PhotoImage(file=os.path.abspath("joe.gif") , master=root) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3393, in __init__    Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3349, in __init__    self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "C:\Program Files\Notepad++\joe.gif": no such file or directory

【问题讨论】:

  • 不清楚你在问什么。您是在问如何在 notepad++ 中打开文件,或者如何在 python/tkinter 脚本中打开文件?您是否知道相对路径是相对于 当前工作目录 而可能并不总是与脚本所在的目录相同?
  • 好的,所以这与 tkinter 完全无关。
  • 对,但是这个问题可以用一个简单的open 语句来复制。 Tkinter 完全不相关。

标签: python notepad++ relative-path


【解决方案1】:

您需要让 NPP 运行脚本并正确设置 working directory,即到您的文件路径。

有一个answer here :只需将正在运行的命令更改为

cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)" 

【讨论】:

  • 我确实相信这是您真正想要的答案。从回溯来看,您的程序正在以“C:\Program Files\Notepad++”作为工作目录运行;答案中的命令将在启动脚本之前更改工作目录。您的回答将工作目录更改为脚本中的硬编码目录。
  • 你是对的。我的回答将迫使我将 os 导入每个包含图像的文件,而您的回答修复了我的 NPP F5 命令,这与最终用户无关,也不会将任何内容硬编码到我的文件中,除了相对路径。这就是我一直在寻找的答案。谢谢。
【解决方案2】:

Here's 答案。用 Python 完成这一切,所以我不必更改 NPP:

import os
os.chdir("C:/tkinter_code")

通过添加 Python 方向,文件现在可以从 NPP F5、图像和所有文件中正确打开。

【讨论】:

  • 但是现在你的程序和图像不能移动到另一个目录而不破坏它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 2012-04-09
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
相关资源
最近更新 更多