0

Edit

Added more code to illustrate my problem better.

Added more examples of what I have tried

I am writing for several packages that require callback functions to be passed as a string. This works fine when I'm sticking to functions only, but I can't seem to find a way to pass self into a function, when I use class's, but still maintaining the string constraint

class MyClass():
   def __init__():
       #Code here

   def UI(self):
       tde4.setWidgetCallbackFunction(req, 'button_name', "function")
       # tde4 is the 3Dequalizer base functions

   def function(self, req, widget, action): # req, widget and action are passed from the 3DEqualizer function
       # req - Is the 'Requester', basically the GUI Window
       # widget - Is the gui element, in this case the button
       # action - Not quite sure what this is, I never used it.
       # Code here

I've tried using:

tde4.setWidgetCallbackFunction(req, 'button_name', "self.function")

but it just states that self cannot be found

tde4.setWidgetCallbackFunction(req, 'button_name', self.function)

but it states it requires a string

tde4.setWidgetCallbackFunction(req, 'button_name', getattr(self, 'function'))

but that doesn't like that a class instance was passed

tde4.setWidgetCallbackFunction(req, 'button_name', str(getattr(self,'function')))

this loads the UI, but when called it throws out a crazy unicode error

tde4.setWidgetCallbackFunction(req, 'button_name', getattr(self,'function')() )

this calls the function, but just uses the result from that function as the new method to call. As I'm not returning anything, it just throws a None type error

Many thanks in advance!

Elliot
  • 21
  • 5
  • 2
    What packages? Name them. – Dan D. Oct 22 '15 at 11:54
  • see if this [SO QA](http://stackoverflow.com/questions/3061/calling-a-function-of-a-module-from-a-string-with-the-functions-name-in-python) helps – Pynchia Oct 22 '15 at 12:02
  • I think that might be it, I'm just error checking now, but it seems to be calling the function correctly. Regarding software, Maya and 3DEqualizer. – Elliot Oct 22 '15 at 13:45
  • Not quite there yet, it seems to call immediately rather than respecting the callback. I think this might be a good excuse to start learning PyQT in order to avoid default gui items. – Elliot Oct 22 '15 at 13:58

1 Answers1

0

In 3dequalizer to launch your GUI, at the bottom of the script you'll instantiate your object by doing something like:

do = MyClass()
do.main()

So, in order to run your callback you put this in the requester:

tde4.setWidgetCallbackFunction(req, 'button_name', "do.function")

This works for my tools, hope it helps you!

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • @SurajRao Cheers for the edit, I'd written it like that but when posted it was as you saw it... First time answer, will make sure I use formatting correctly next time! – Craig Allison May 18 '18 at 13:22