Tkinter string callbacks Tkinter string callbacks tkinter tkinter

Tkinter string callbacks


I need a way to callback functions via a string.

From the comments to your question I understand that you'd like to do something like:

# ...callback_str = getcallback_str() # e.g., 'self.logic.account_new'callback = eval_dottedname(self, callback_str)`

In this case eval_dottedname() function could be implemented as:

def eval_dottedname(obj, dottedname):    if dottedname.partition(".")[0] != 'self': # or some other criteria                                               # to limit the context        raise ValueError    return reduce(getattr, dottedname.split('.')[1:], obj)

A better approach would be to limit string callbacks to simple identifiers and use a dispatch table like stdlib's cmd module:

  def dispatch(self, callback_str):      return getattr(self, 'do_' + callback_str, self.default)()        def do_this(self):      pass  def do_that(self):      pass