获取记事本++以使用相对路径进行图像

路德

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

仅当我按照顶部未注释的代码行所示硬编码完整路径时,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
AKX

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章