Author terry.reedy
Recipients terry.reedy
Date 2017-07-05.06:03:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1499234606.42.0.386871261381.issue30853@psf.upfronthosting.co.za>
In-reply-to
Content
configdialog.ConfigDialog creates about 20 tk Variables corresponding to configuration options.  It defines a var_changed_xyz callback for each xyz variable.  6 have a common template.  ConfigDialog turns tracing on in attach_var_callbacks and off in remove_var_callbacks.  Each of these two functions has the callbacks set hard-coded.

Proposal: Factor out what can.  Register callbacks in set when var created.  Iterate register to attach and remove callbacks.  Something like

def IVariable:
    changes = <instance of changes>  # set externally somehow
    var = set()

    def register(self, callback)
        if isinstance(callback, tuple):
            self.callback = default_callback
            self.args = callback
        else:
            self.callback = callback
        self.vars.add()

    def default_callback(self):  # Used for 6 vars.
        changes.add_item(*self.args, self.get())

    @classmethod
    def attach(cls):
        for var in cls.vars:
        var.trace_add('write', self.callback)

    @classmethod
    def remove(cls):
        for var in cls.vars:
            var.trace_remove('write', var.trace_info()[0][1])
        cls.vars = set()

To get String/Int/BooleanVars, maybe this will work.  (I have only toyed with multiple inheritance.)

class IString(tk.StringVar, IVariable):
    def __init__(self, callback).
        StringVar.__init()  # Possibly not needed with no value to pass.
        self.register(callback)
History
Date User Action Args
2017-07-05 06:03:26terry.reedysetrecipients: + terry.reedy
2017-07-05 06:03:26terry.reedysetmessageid: <1499234606.42.0.386871261381.issue30853@psf.upfronthosting.co.za>
2017-07-05 06:03:26terry.reedylinkissue30853 messages
2017-07-05 06:03:25terry.reedycreate