Skip to content
Snippets Groups Projects
Commit aa454c8b authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Widget states are correctly initialised (i.e. if the initial property value is invalid)

parent 908c3a29
No related branches found
No related tags found
No related merge requests found
......@@ -216,7 +216,8 @@ class TkVarProxy(object):
listeners are notified of the variable value change.
"""
valid = True
valid = True
listeners = self.changeListeners.items()
# This is silly. Tkinter allows Boolean/Int/Double
# variables to be set to invalid values (e.g. it
......@@ -233,8 +234,14 @@ class TkVarProxy(object):
# failing type cast. Ugly.
except: newValue = self.tkVar._tk.globalgetvar(self.name)
# if the new value is valid, save it as the last
# known good value
# print a log message if the value has changed
if newValue != self.lastValue:
log.debug(
'Variable {} changed: {} (valid: {}, {} listeners)'.format(
self.name, newValue, valid, len(listeners)))
# if the new value is valid, save
# it as the last known good value
try:
self.tkProp.validate(self.owner, newValue)
self.lastValue = newValue
......@@ -242,11 +249,6 @@ class TkVarProxy(object):
except ValueError:
valid = False
listeners = self.changeListeners.items()
log.debug('Variable {} changed: {} (valid: {}, {} listeners)'.format(
self.name, newValue, valid, len(listeners)))
# Notify all listeners about the change, ignoring
# any errors - it is up to the listeners to ensure
# that they handle invalid values
......@@ -347,6 +349,23 @@ class PropertyBase(object):
self.changeListeners[instance].pop(name)
def forceValidation(self, instance):
"""
Forces validation of this property value, for the current instance.
This will result in any registered listeners being notified.
"""
varProxies = instance.getTkVar(self.label)
# getTkVar returns either a TkVarProxy object, or a
# list of TkVarProxy objects (it should do, anyway).
if isinstance(varProxies, TkVarProxy):
varProxies = [varProxies]
for var in varProxies:
var._traceCb()
def _varChanged(self, value, valid, instance, tkProp, name):
"""
This function is registered with the TkVarProxy object (or
......
......@@ -71,6 +71,10 @@ def _setupValidation(widget, propObj, tkProp, tkVar):
listenerName = 'ChangeBGOnValidate_{}'.format(tkVar.name)
tkVar.addListener(listenerName, _changeBGOnValidate)
# Validate the initial property value,
# so the background is appropriately set
tkProp.forceValidation(propObj)
# The _lastFilePathDir variable is used to retain the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment