Search code examples
pythonfunctionuser-inputeasygui

Running a function in Python if a user chooses it (EasyGui)


I'm using EasyGui to allow a user to select multiple options. Each option is a function which they can run if they select it. I'm trying to use dictionaries as suggested in other threads but I'm having trouble implementing it (Module object is not callable error). Is there something I'm missing?

from easygui import *
import emdtest1
import emdtest2
import emdtest3

EMDTestsDict = {"emdtest1":emdtest1,
                "emdtest2":emdtest2,
                "emdtest3":emdtest3}

def main():
    test_list = UserSelect()
    for i in range(len(test_list)):
        if test_list[i] in EMDTestsDict.keys():
            EMDTestsDict[test_list[i]]()

def UserSelect():
    message = "Which EMD tests would you like to run?"
    title = "EMD Test Selector"
    tests = ["emdtest1",
             "emdtest2",
             "emdtest3"]
    selected_master = multchoicebox(message, title, tests)
    return selected_master

if __name__ == '__main__':
    main()

Solution

  • You're putting modules into the dict, when you want to put functions in it. What you're doing is the equivalent of saying

    import os
    os()
    

    Which, of course, makes no sense. If emdtest1, emdtest2, and emdtest3 are .py files with functions in them, you want:

    from emdtest1 import function_name
    

    Where function_name is the name of your function.