0
    def runtest(testname):
        usrdef_sship = sship_e.get()
        usrdef_username = username_e.get()
        usrdef_pass = pass_e.get()
        testresult = cpdiag.testname(cpdiag.ssh(usrdef_sship, usrdef_username, usrdef_pass))
        messagebox.showinfo( "Results", testresult)

    #Button for disk Test
    disktest = Button(top, text="Disk Test", command = lambda: runtest("diskTest")
    disktest.grid(row=3,column=0, sticky=W)
    #Button for cpwd Test
    cpwdtest = Button(top, text="CPWD Test", command = lambda: runtest("cpwdTest")
    cpwdtest.grid(row=4,column=0, sticky=W)

Tried to shrink the code for what is necessary. Full code at https://github.com/elta13/cpdiag. The above code, I've been banging my head on it for some time. I'm fairly new to python and have been learning as needed during this, my first project.

I'm using Tkinter to display 3 entry fields which are working to obtain usrdef variables. Then I'm using several buttons, each will call a function from another local file cpdiag.py. Right now, I have been rewriting function for each remote function I call with the only thing that changes being the line with cpdiag."testname".(cpdaig.ssh"etc".)

Current problems: If I pass on the button runtest(diskTest), inside runtuest this prints as "function diskTest at x0xxxxx" And the code crashes with it trying to call "cpdiag.testname" which doesn't exist in the other local file cpdiag.py.

So then I figured out I could pass runtest("diskTest"), and it would print inside of runtest(). But runtest still tries to call "cpdiag.testname" from the other file. I'm realizing at this point, I don't understand how to call something remotely with a local variable.

So then I tried passing runtest("cpdiag.diskTest"), for which I incurred "'str' can't be called" or of the like.

How can I pass "testname" into runtest(), where "testname" is a function defined in other file cpdiag.py?!?!?

Joshua Hatter
  • 93
  • 1
  • 1
  • 8
  • I think I've read the top answer like 20 times on that one, I don't think I comprehend what 'bar' is in the example. – Joshua Hatter Mar 21 '17 at 13:03
  • bar is the name of the function of foo. In you example, it will be `testresult = getattr(cpdiag, testname)(cpdiag.ssh(usrdef_sship, usrdef_username, usrdef_pass))` – Emin Mastizada Mar 21 '17 at 13:05
  • Ok so first off, thanks a million it works! I don't understand how yet but, soon enough! Much thanks for explaining a little further for my scenario! – Joshua Hatter Mar 21 '17 at 13:10
  • getattr function helps you to get method from cpdiag library as callable function using method's name. Then you make call on it as usual functions. – Emin Mastizada Mar 21 '17 at 13:13

1 Answers1

0

You can get the instance of the method you want to invoke on the object cpdiag using getattr builtin then invoke the method. Something like below:

your_function = getattr(cpdiag, testname) # Get the method first
#  Now invoke
your_function(cpdiag.ssh(usrdef_sship, usrdef_username, usrdef_pass))
gipsy
  • 3,859
  • 1
  • 13
  • 21