亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 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 關注
  • 280 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號