简体   繁体   English

Python 3-将函数的Return存储为变量,以便以后重复使用

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

I am still pretty new to Python and programming in general, but as a method to learn some more Python, and just tinkering around with some Windows Registry data, I started working on a very simple tkinter and Python3 data extractor. 我仍然对Python和编程尚不陌生,但是作为一种学习更多Python的方法,并且只是修改一些Windows Registry数据,我开始研究非常简单的tkinter和Python3数据提取器。

I was stuck getting output from a function to store as a variable in some manner to use later, and sometimes used repeatedly. 我一直无法从函数中获取输出,以某种方式存储为变量以供以后使用,有时会重复使用。 There are just a few buttons to locate paths, save the file path, and I want to use that file path in another function to grab data from files. 只有几个按钮可以找到路径,保存文件路径,我想在另一个函数中使用该文件路径从文件中获取数据。

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

Then I waould like to use the Return data (sw_path1) which is just a local system path, into another function that will be called later. 然后,我想将Return数据(sw_path1)(仅是本地系统路径)用于另一个函数,稍后再调用它。 For example: 例如:

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

All the functions work separately, but getting the return of one into the other to use later has been a hurdle. 所有功能都是单独工作的,但是要使一个功能返回另一个功能以供以后使用一直是一个障碍。 I have tried to store this using another variable, such as 我试图使用另一个变量来存储它,例如

Var1  = sw_path1

But this becomes an unresolved reference outside of the function itself 但这成为函数本身之外的未解决的引用

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

**** Update Adding the variable outside of the function, such as: ****更新在函数外部添加变量,例如:

    sw_path1 = None

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

Does not store the variable, once it is obtained, it is always None. 不存储变量,一旦获得变量,则始终为None。

You need to make the variable sw_data a global variable. 您需要将变量sw_data设置为全局变量。 Currently it is a function level variable. 当前,它是一个功能级别变量。 To do so declare your variable outside of the function. 为此,请在函数外部声明变量。
You can then call sw_path1 in any other function 然后,您可以在任何其他函数中调用sw_path1

Hope this helps! 希望这可以帮助!

define the variable at module level. 在模块级别定义变量。 if it is set in thesw_click functions, the value can still be used in the swpull_click function. 如果在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

Setting the variable to None prior to the function being executed allows for it to be called into the function using the global setting. 在执行函数之前将变量设置为None允许使用全局设置将其调用到函数中。 By using global within the function, as long as that variable is updated within the function, that global variable previously set to None, will be updated. 通过在函数内使用全局变量,只要在函数内更新该变量,先前设置为“无”的全局变量将被更新。 This is then stored for later use, unless another function or process clears or replaces it. 然后将其存储以供以后使用,除非另一个功能或过程将其清除或替换。

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()

The three print functions used (2 in function1, and 1 in function2) would return in the order of: 使用的三个打印功能(function1中为2,function2中为1)将按以下顺序返回:

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

Removing the preset Var1 = None results in the script running, but when calling function 1, there will be a NameError, NameError: name 'var1' is not defined 删除预设Var1 = None导致脚本运行,但是调用函数1时将出现NameError, NameError: name 'var1' is not defined

Removing global var1 from function1, with var1 still set outside to None, the script will still run but when using function1, it will throw an UnboundLocalError: local variable 'var1' referenced before assignment error at the first line the variable is seen in the functions. 从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