为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

郎朗坤 2021-09-28 16:39:19
我可以使用 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 tkroot = 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 = img4logoimage.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 directoryTraceback (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
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您需要让 NPP 在正确设置工作目录(即文件路径)的情况下运行脚本。

这里有一个答案:只需将正在运行的命令更改为

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


查看完整回答
反对 回复 2021-09-28
?
慕村9548890

TA贡献1884条经验 获得超4个赞

这是答案。要在 Python 中完成这一切,所以我不必更改 NPP:

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

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


查看完整回答
反对 回复 2021-09-28
  • 2 回答
  • 0 关注
  • 258 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号