【发布时间】: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