简体   繁体   中英

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.

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. 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.

You need to make the variable sw_data a global variable. 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

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_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. 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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM