Use properties rather than overwriting __setattr__
This commit is contained in:
parent
ebefdfb5a9
commit
d6c593f035
@ -25,10 +25,10 @@ class Text(TextBase):
|
|||||||
"""Text displayed in the statusbar.
|
"""Text displayed in the statusbar.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
normaltext: The "permanent" text. Never automatically cleared.
|
_normaltext: The "permanent" text. Never automatically cleared.
|
||||||
temptext: The temporary text to display.
|
Accessed via normaltext property.
|
||||||
_initializing: True if we're currently in __init__ and no text should
|
_temptext: The temporary text to display.
|
||||||
be updated yet.
|
Accessed via temptext property.
|
||||||
|
|
||||||
The temptext is shown from StatusBar when a temporary text or error is
|
The temptext is shown from StatusBar when a temporary text or error is
|
||||||
available. If not, the permanent text is shown.
|
available. If not, the permanent text is shown.
|
||||||
@ -36,22 +36,33 @@ class Text(TextBase):
|
|||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._initializing = True
|
self._normaltext = ''
|
||||||
self.normaltext = ''
|
self._temptext = ''
|
||||||
self.temptext = ''
|
|
||||||
self._initializing = False
|
|
||||||
|
|
||||||
def __setattr__(self, name, val):
|
@property
|
||||||
"""Overwrite __setattr__ to call _update_text when needed."""
|
def normaltext(self):
|
||||||
super().__setattr__(name, val)
|
"""Getter for normaltext so we can define a setter."""
|
||||||
if not name.startswith('_') and not self._initializing:
|
return self._normaltext
|
||||||
|
|
||||||
|
@normaltext.setter
|
||||||
|
def normaltext(self, val):
|
||||||
|
"""Setter for normaltext to update text display after setting."""
|
||||||
|
self._normaltext = val
|
||||||
|
self._update_text()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def temptext(self):
|
||||||
|
"""Getter for temptext so we can define a setter."""
|
||||||
|
return self._temptext
|
||||||
|
|
||||||
|
@temptext.setter
|
||||||
|
def temptext(self, val):
|
||||||
|
"""Setter for temptext to update text display after setting."""
|
||||||
|
self._temptext = val
|
||||||
self._update_text()
|
self._update_text()
|
||||||
|
|
||||||
def _update_text(self):
|
def _update_text(self):
|
||||||
"""Update QLabel text when needed.
|
"""Update QLabel text when needed."""
|
||||||
|
|
||||||
Called from __setattr__ if a text property changed.
|
|
||||||
"""
|
|
||||||
for text in [self.temptext, self.normaltext]:
|
for text in [self.temptext, self.normaltext]:
|
||||||
if text:
|
if text:
|
||||||
self.setText(text)
|
self.setText(text)
|
||||||
|
Loading…
Reference in New Issue
Block a user