From 6bfe0a752263dfd44c0330339627375baa5ca84b Mon Sep 17 00:00:00 2001 From: Eugenio Parodi Date: Tue, 3 Jan 2023 12:50:27 +0100 Subject: [PATCH] Added Button fields, removed button text property --- TermTk/TTkWidgets/button.py | 51 +++++++++++++++++++++--- TermTk/TTkWidgets/tabwidget.py | 4 +- tests/test.generic.004.footprint.py | 62 +++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 tests/test.generic.004.footprint.py diff --git a/TermTk/TTkWidgets/button.py b/TermTk/TTkWidgets/button.py index 283af72d..94a4a1de 100644 --- a/TermTk/TTkWidgets/button.py +++ b/TermTk/TTkWidgets/button.py @@ -52,7 +52,7 @@ class TTkButton(TTkWidget): :param bool checked: checked status if the button is checkable, defaults to "False" :type checked: bool, optional - :param bool checkable: define if the burtton is checkable, defaults to "False" + :param bool checkable: define if the button is checkable, defaults to "False" :type checkable: bool, optional :param TTkColor color: the color of the border of the button, defaults to :class:`~TermTk.TTkTheme.theme.TTkTheme.buttonTextColor` @@ -96,7 +96,6 @@ class TTkButton(TTkWidget): self.setDefaultSize(kwargs, 2 + textWidth, 3 if self._border else 1 ) TTkWidget.__init__(self, *args, **kwargs) - self._name = kwargs.get('name' , 'TTkButton' ) # Define Signals self.clicked = pyTTkSignal() self.toggled = pyTTkSignal(bool) @@ -122,16 +121,36 @@ class TTkButton(TTkWidget): self.setFocusPolicy(TTkK.ClickFocus + TTkK.TabFocus) def isCheckable(self): + ''' This property holds whether the button is checkable + + :return: bool + ''' return self._checkable def setCheckable(self, ch): + ''' Enable/Disable the checkable property + + :param ch: Checkable + :type ch: bool + ''' self._checkable = ch self.update() def isChecked(self): + ''' This property holds whether the button is checked + + Only checkable buttons can be checked. By default, the button is unchecked. + + :return: bool + ''' return self._checked def setChecked(self, ch): + ''' Set the checked status + + :param ch: Checked + :type ch: bool + ''' self._checked = ch self.toggled.emit(self._checked) self.update() @@ -150,12 +169,20 @@ class TTkButton(TTkWidget): self._borderColor = color self.update() - @property def text(self): + ''' This property holds the text shown on the button + + :return: :class:`~TermTk.TTkCore.string.TTkString` + ''' return self._text - @text.setter - def text(self, text): + def setText(self, text): + ''' This property holds the text shown on the button + + :param text: + :type text: :class:`~TermTk.TTkCore.string.TTkString` + ''' + if self._text == text: return self._text = TTkString(text) self.setMinimumSize(self._text.termWidth()+2, 1) self.update() @@ -232,3 +259,17 @@ class TTkButton(TTkWidget): self._canvas.drawText(pos=(1+text.termWidth(),y), color=borderColor ,text=']') self._canvas.drawText(pos=(1,y) ,text=text) + _ttkProperties = { + 'text' : { + 'init': {'name':'text', 'type':TTkString }, + 'get': {'cb':text, 'type':TTkString } , + 'set': {'cb':setText, 'type':TTkString } }, + 'checkable' : { + 'init': {'name':'checkable', 'type':bool }, + 'get': {'cb':isCheckable, 'type':bool } , + 'set': {'cb':setCheckable, 'type':bool } }, + 'checked' : { + 'init': {'name':'checked', 'type':bool }, + 'get': {'cb':isChecked, 'type':bool } , + 'set': {'cb':setChecked, 'type':bool } }, + } \ No newline at end of file diff --git a/TermTk/TTkWidgets/tabwidget.py b/TermTk/TTkWidgets/tabwidget.py index 2ece703f..0fc16fff 100644 --- a/TermTk/TTkWidgets/tabwidget.py +++ b/TermTk/TTkWidgets/tabwidget.py @@ -61,7 +61,7 @@ class TTkTabButton(TTkButton): self._closable = kwargs.get('closable', False) self.closeClicked = pyTTkSignal() TTkButton.__init__(self, *args, **kwargs) - size = len(self.text) + 2 + size = self.text().termWidth() + 2 if self._closable: size += 3 self._closeButton = TTkButton(parent=self, border=False, text="x", pos=(size-4,1 if self._border else 0), size=(3,1)) @@ -122,7 +122,7 @@ class TTkTabButton(TTkButton): small=(not self._border), sideEnd=self._sideEnd, status=self._tabStatus, color=self._borderColor ) - self._canvas.drawText(pos=(1,1 if self._border else 0), text=self.text, color=self.color()) + self._canvas.drawText(pos=(1,1 if self._border else 0), text=self.text(), color=self.color()) class _TTkTabMenuButton(TTkMenuButton): def __init__(self, *args, **kwargs): diff --git a/tests/test.generic.004.footprint.py b/tests/test.generic.004.footprint.py new file mode 100644 index 00000000..996e5930 --- /dev/null +++ b/tests/test.generic.004.footprint.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2021 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys + +class A(): + def a(self): print(f"{self=}") +class B(A): pass +class C(B): pass + +class Aa(): + _euVars = {'aa':1, 'ab':2} + +class Bb(Aa): + _euVars = {'ba':1, 'bb':2} + +class Cc(Bb): + _euVars = {'ca':1, 'cb':2} + +c = C() +cc = Cc() + +print(cc) +print(cc.__class__.__name__) +print(Cc.__mro__) + +print(f"{sys.getsizeof(c)=}") +print(f"{sys.getsizeof(cc)=}") + +print(f"{sys.getsizeof(A)=}") +print(f"{sys.getsizeof(B)=}") +print(f"{sys.getsizeof(C)=}") + +print(f"{sys.getsizeof(Aa)=}") +print(f"{sys.getsizeof(Bb)=}") +print(f"{sys.getsizeof(Cc)=}") + + +for co in reversed(type(cc).__mro__): + if hasattr(co,'_euVars'): + print(f"{co} -> {co.__name__} -> {co.__class__} -> {co.__class__.__name__} -> {co._euVars}") \ No newline at end of file