From 2b47c02f6b8a2995792496be4031e72a026a1114 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 2 Jan 2022 17:58:48 +0800 Subject: [PATCH] bugfix --- kivyblocks/price.py | 47 ++++++++++++++++++++++++++++++++++++++++++ kivyblocks/register.py | 1 + 2 files changed, 48 insertions(+) create mode 100644 kivyblocks/price.py diff --git a/kivyblocks/price.py b/kivyblocks/price.py new file mode 100644 index 0000000..3c497d2 --- /dev/null +++ b/kivyblocks/price.py @@ -0,0 +1,47 @@ + +from kivy.uix.label import Label +from kivy.properties import NumericProperty +from kivy.factory import Factory + +from kivyblocks.uitype import view_register, get_value + +class Price(Label): + dec = NumericProperty(4) + old_price = NumericProperty(None) + price = NumericProperty(None) + nor_color = ColorProperty([0.9,0.9,0.9,1]) # white + inc_color = ColorProperty([0.9,0,0,1]) # red + dec_color = ColorProperty([0,0.9,0,1]) # green + def __init__(self, **kw): + super(Price, self).__init__(text='', **kw) + + def on_price(self, price): + if self.price is None: + return + if not isinstance(self.price, float) and \ + not isinstance(self.price, int): + return + + f = '%%.0%df' % self.dec + self.text = f % price + if self.old_price is None: + self.color = self.nor_color + elif self.price > self.old_price: + self.color = self.inc_color + elif self.price < self.old_price: + self.color = self.dec_color + else: + self.color = nor_color + self.old_price = self.price + +Factory.register('Price', Price) + +def build_view_price_widget(desc, rec=None): + v = get_value(desc, rec=rec) + return Factory.Price(price=v, + font_size=CSize(1), + halign='right', + valign='middle' + ) + +view_register('price', build_view_price_widget) diff --git a/kivyblocks/register.py b/kivyblocks/register.py index 37e321d..90acc2f 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -41,6 +41,7 @@ from .paging import PageLoader from .dateinput import DateInput from .block_test import BlockTest from .hirarchy import Hirarchy +from .price import Price r = Factory.register if kivy.platform in ['win','linux', 'macosx']: