| Philipp Le | cfb6d2a | 2022-04-26 22:08:48 +0200 | [diff] [blame] | 1 | # SPDX-License-Identifier: MPL-2.0 |
| 2 | # Copyright (c) 2022 Philipp Le <philipp@philipple.de>. |
| 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | |
| 7 | |
| 8 | from tkinter import ttk, StringVar |
| 9 | |
| 10 | from .base import make_config_widget_base |
| 11 | |
| 12 | |
| 13 | class StrWidget(make_config_widget_base(ttk.Entry)): |
| 14 | MODEL_TYPE = str |
| 15 | |
| 16 | def sync_out(self) -> None: |
| 17 | try: |
| 18 | setattr(self.model, self.model_key, self._stringvar.get()) |
| 19 | self.notify_change() |
| 20 | except ValueError: |
| 21 | # Tolerate temporarily invalid values |
| 22 | pass |
| 23 | |
| 24 | def sync_in(self) -> None: |
| 25 | val = getattr(self.model, self.model_key) |
| 26 | self._stringvar.set(str(val)) |
| 27 | |
| 28 | def _config_widget(self) -> None: |
| 29 | self._stringvar = StringVar(self.parent) |
| 30 | self.sync_in() |
| 31 | self.configure(textvariable=self._stringvar) |
| 32 | self.bind('<KeyRelease>', lambda e: self.sync_out()) |