猿问

获取 Notepad++ 以使用图像的相对路径

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


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


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


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


郎朗坤
浏览 259回答 2
2回答

BIG阳

您需要让 NPP 在正确设置工作目录(即文件路径)的情况下运行脚本。这里有一个答案:只需将正在运行的命令更改为cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)"

慕村9548890

这是答案。要在 Python 中完成这一切,所以我不必更改 NPP:import&nbsp;os os.chdir("C:/tkinter_code")通过添加 Python 方向,文件现在可以从 NPP F5、图像等正确打开。
随时随地看视频慕课网APP

相关分类

Python
我要回答