獲取python函數的變量 (Get a variable of python function)


問題描述

獲取python函數的變量 (Get a variable of python function)

我是從python開始的,所以如果我使用的詞彙不合適,請見諒。我創建了一個函數,我想在一個變量中恢復文件所在的位置(它在計算機中的 URL)。

有人能告訴我這怎麼可能嗎?

提前謝謝

from tkinter import*
from tkinter import filedialog
from tkinter.messagebox import*
import tkinter.filedialog

Tk().withdraw()

def insert_photo():
    photo = tkinter.filedialog.askopenfilename(title="Ouvrir une image",filetypes=[('Jpg files','.jpg'),('Gif files','.gif'),('Bmp files','.bmp'),('Png files','.png'),('Tiff files','.tif'),('All files','.*')])
    return photo

其實我只是想在圖像文件的路徑中獲取一個變量,以便我可以在另一個腳本中更容易地調用它。

我有試過 print(os.path.realpath(__file__)) 但問題是,即使 tkinter 窗口長矛。我只是希望該函數在 shell 中執行,並且只有當我調用它時,探索窗口才會打開,並且我可以在變量中獲取路徑。


參考解法

方法 1:

I am not sure exactly what your problem is. I have run your code and it works. Are you after a UNC path?

Have you tried:

>>> p = insert_photo():
>>> print(p)

You seem to have your path already.

If you are trying to open the file dialog in a specific folder you can change the current working directory with os.chdir(PATH).

If you only want the folder the image is in you can do os.path.split(PATH)[0]

(by MagicLudododell)

參考文件

  1. Get a variable of python function (CC BY‑SA 2.5/3.0/4.0)

#Variables #Python #function #tkinter #messagebox






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論