[issue30853] IDLE: configdialog -- factor out Variable subclass

Terry J. Reedy report at bugs.python.org
Wed Jul 5 02:03:26 EDT 2017


New submission from Terry J. Reedy:

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)

----------
assignee: terry.reedy
components: IDLE
messages: 297703
nosy: terry.reedy
priority: normal
severity: normal
stage: test needed
status: open
title: IDLE: configdialog -- factor out Variable subclass
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30853>
_______________________________________


More information about the Python-bugs-list mailing list