簡體   English   中英

Python 3-將函數的Return存儲為變量,以便以后重復使用

[英]Python 3 - Store a function's Return as a variable to use repeatedly later

我仍然對Python和編程尚不陌生,但是作為一種學習更多Python的方法,並且只是修改一些Windows Registry數據,我開始研究非常簡單的tkinter和Python3數據提取器。

我一直無法從函數中獲取輸出,以某種方式存儲為變量以供以后使用,有時會重復使用。 只有幾個按鈕可以找到路徑,保存文件路徑,我想在另一個函數中使用該文件路徑從文件中獲取數據。

def sw_click():
    sw_path1 = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s')
    swP_label.config(text=swpath1)
    return sw_path1

然后,我想將Return數據(sw_path1)(僅是本地系統路徑)用於另一個函數,稍后再調用它。 例如:

def swpull_click():
    swinfo = *function_pullkey (sw_path1)   #Using another function 
    Return sw_data    # again as a variable for later use

所有功能都是單獨工作的,但是要使一個功能返回另一個功能以供以后使用一直是一個障礙。 我試圖使用另一個變量來存儲它,例如

Var1  = sw_path1

但這成為函數本身之外的未解決的引用

任何幫助將不勝感激。 謝謝

****更新在函數外部添加變量,例如:

    sw_path1 = None

    def software_click():
    global sw_path1
    tkinter.filedialog.askopenfilename(initialdir='')
    softwareP_label.config(text=sw_path1)
        return sw_path1

不存儲變量,一旦獲得變量,則始終為None。

您需要將變量sw_data設置為全局變量。 當前,它是一個功能級別變量。 為此,請在函數外部聲明變量。
然后,您可以在任何其他函數中調用sw_path1

希望這可以幫助!

在模塊級別定義變量。 如果在sw_click函數中設置了該值,則該值仍可以在swpull_click函數中使用。

sw_path1 = None

def sw_click():
    # the global keyword stats that a module level variable can be set inside a function
    global sw_path1
    sw_path1 = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s')
    swP_label.config(text=swpath1)
    return sw_path1

def swpull_click():
    swinfo = *function_pullkey (sw_path1)   #Using another function 
    return sw_data    # again as a variable

在執行函數之前將變量設置為None允許使用全局設置將其調用到函數中。 通過在函數內使用全局變量,只要在函數內更新該變量,先前設置為“無”的全局變量將被更新。 然后將其存儲以供以后使用,除非另一個功能或過程將其清除或替換。

import tkinter
from tkinter import filedialog

root = tkinter.Tk()

# Setting a variable to none, that can be used, updated, etc.
var1 = None

# So here a user would select their file, which would update var 1 from None, to the results
# This can be handy for validations using if statements to show the variable has not been updated
def function_1():
    global var1   # Must be set to global in the function to be able to access var1 and update
    print(var1)
    var1 = tkinter.filedialog.askopenfilename(initialdir='C:')
    print(var1)

# Updating the variable in the manner above allows for it to be updated repeatedly
# Now that updated variable can be used in function2

def function_2():
    print(var1)

button1 = tkinter.Button(root, text="get the path", command=function_1)
button2 = tkinter.Button(root, text="do something with it", command=function_2)

button1.pack()
button2.pack()
root.mainloop()

使用的三個打印功能(function1中為2,function2中為1)將按以下順序返回:

None
C:/Windows/regedit.exe
C:/Windows/regedit.exe

刪除預設Var1 = None導致腳本運行,但是調用函數1時將出現NameError, NameError: name 'var1' is not defined

從function1刪除global var1 ,並且var1仍設置為None外,腳本仍將運行,但使用function1時,它將引發UnboundLocalError: local variable 'var1' referenced before assignment錯誤UnboundLocalError: local variable 'var1' referenced before assignment在第一行中UnboundLocalError: local variable 'var1' referenced before assignment ,該變量在函數中可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM