简体   繁体   中英

How to pass a result from a function that requires GUI input (python)?

I'm writing a python script that takes user input through a GUI and then passes that along to a function that basically reads through text files and checks that what the user requested is there. This function returns an array with True or False for each check.

I want to use this array in a different function (def markup()), but if I call it without giving the function the user input, I get an error.

Is there a way for me to store the results of this function and pass it without needing the user input each time?

Pseudo code:

def clickButton():

    userInput = [A,B,C,D,E]
    textCheck(userInput)

def textCheck(userInput):

    *code for checking text creates an array named allResults*
    return allResults

def markup():

    results = textCheck()
    print(results)

You need to manage allResults as a persistent object. One way is to pass the results everywhere as a parameter, such that whatever thread is executing always has a handle to the list. Another way is to ( shudder ) make it a global variable -- this is somewhat dangerous as a habit, but may be the easiest for you to implement and maintain. You can also create a Results class and instantiate an object that persists as long as you need it.

I can't recommend one over another without having the flow of the main program.

I ended up calling markup(allResults) at the end of textChecker(). It worked.

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