diff options
author | emkael <emkael@tlen.pl> | 2019-06-23 13:54:15 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-06-23 13:54:15 +0200 |
commit | 5d9ccd1ddee2aa4b9e651b68d20dabbc5fba3c4c (patch) | |
tree | 3ac297c8863f5a2ceab4e0192c9da7ebf27f69cd /jfr_playoff | |
parent | e932dc7e2e45cdd56f7ef5148335f758dbb26a7b (diff) |
Text widget with the ability to bind a tk Variable
Diffstat (limited to 'jfr_playoff')
-rw-r--r-- | jfr_playoff/gui/frames/__init__.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/jfr_playoff/gui/frames/__init__.py b/jfr_playoff/gui/frames/__init__.py index 50860b1..623b5bd 100644 --- a/jfr_playoff/gui/frames/__init__.py +++ b/jfr_playoff/gui/frames/__init__.py @@ -326,3 +326,37 @@ class RefreshableOptionMenu(ttk.OptionMenu): def getOptions(self): pass + +class TraceableText(tk.Text): + def __init__(self, *args, **kwargs): + self._variable = None + self._variableLock = False + if 'variable' in kwargs: + self._variable = kwargs['variable'] + del kwargs['variable'] + tk.Text.__init__(self, *args, **kwargs) + if self._variable is not None: + self._orig = self._w + '_orig' + self.tk.call('rename', self._w, self._orig) + self.tk.createcommand(self._w, self._proxy) + self._variable.trace('w', self._fromVariable) + + def _fromVariable(self, *args): + if not self._variableLock: + self._variableLock = True + self.delete('1.0', tk.END) + self.insert(tk.END, self._variable.get()) + self._varaibleLock = False + + def _proxy(self, command, *args): + cmd = (self._orig, command) + args + result = self.tk.call(cmd) + if command in ('insert', 'delete', 'replace') and \ + not self._variableLock: + text = self.get('1.0', tk.END).strip() + self._variableLock = True + self._variable.set(text) + self._variableLock = False + return result + +# TODO: NumericSpinBox instead of getIntVal |