From 44fe243935ea2837bc6286c6bf18ae03b34df362 Mon Sep 17 00:00:00 2001 From: sphh Date: Fri, 15 Nov 2024 01:27:59 +0000 Subject: [PATCH] Adds range field to wx.lib.agw.pygauge.PyGauge format string. (#2583) * Adds range to wx.lib.awg.pygauge.PyGauge format string. The format string can now have fields for `value` and `range`. * Updates the desciption. * Uses field names in default format string (as described). * Removes dependency on the `copy` module. --- wx/lib/agw/pygauge.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/wx/lib/agw/pygauge.py b/wx/lib/agw/pygauge.py index da42e6f6..c7815f9e 100644 --- a/wx/lib/agw/pygauge.py +++ b/wx/lib/agw/pygauge.py @@ -113,7 +113,6 @@ Version 0.1 """ import wx -import copy class PyGauge(wx.Window): @@ -343,7 +342,7 @@ class PyGauge(wx.Window): for i, gradient in enumerate(self._barGradientSorted): c1,c2 = gradient w = rect.width * (float(self._valueSorted[i]) / self._range) - r = copy.copy(rect) + r = wx.Rect(rect) r.width = int(w) dc.GradientFillLinear(r, c1, c2, wx.EAST) else: @@ -351,7 +350,7 @@ class PyGauge(wx.Window): dc.SetBrush(wx.Brush(colour)) dc.SetPen(wx.Pen(colour)) w = rect.width * (float(self._valueSorted[i]) / self._range) - r = copy.copy(rect) + r = wx.Rect(rect) r.width = int(w) dc.DrawRectangle(r) @@ -364,7 +363,8 @@ class PyGauge(wx.Window): if self._drawIndicatorText_drawPercent: drawValue = (float(self._valueSorted[i]) * 100) / self._range - drawString = self._drawIndicatorText_formatString.format(drawValue) + drawString = self._drawIndicatorText_formatString.format( + drawValue, value=drawValue, range=self._range) rect = self.GetClientRect() (textWidth, textHeight, descent, extraLeading) = dc.GetFullTextExtent(drawString) textYPos = (rect.height-textHeight)//2 @@ -392,9 +392,10 @@ class PyGauge(wx.Window): will be used. Usually text would be displayed centered in the control, but if the text font is too large to be displayed (either in width or height) the corresponding coordinate will be set to zero; :param wx.Colour `colour`: the colour with which indication should be drawn, if ``None`` then ``wx.BLACK`` will be used; - :param string `formatString`: a string specifying format of the indication (should have one and only one - number placeholder). If set to ``None``, will use ``{:.0f}`` format string for values and ``{:.0f}%`` - format string for percentages. As described in http://docs.python.org/library/string.html#format-specification-mini-language. + :param string `formatString`: a string specifying format of the indication (could have one and only one unnamed + number placeholder and a `value` and `range` number placeholder). If set to ``None``, will use ``{value:.0f}`` + format string for values and ``{value:.0f}%` format string for percentages. As described in + http://docs.python.org/library/string.html#format-specification-mini-language. .. note:: `formatString` will override addition of percent sign (after value) even if `drawPercent` is ``True``. @@ -423,7 +424,7 @@ class PyGauge(wx.Window): error_occurred = True try: # This is to test if format string is valid. If not, it will be replaced with default one. - formatString.format(12.345) + formatString.format(12.345, value=12.345, range=54.321) error_occurred = False except Exception as e: print(("We have exception: %s"%e)) @@ -434,8 +435,8 @@ class PyGauge(wx.Window): # Here formatString is either valid formatting string, or None in case of error or None passed if formatString is None: if self._drawIndicatorText_drawPercent: - self._drawIndicatorText_formatString = "{:.0f}%" - else: self._drawIndicatorText_formatString = "{:.0f}" + self._drawIndicatorText_formatString = "{value:.0f}%" + else: self._drawIndicatorText_formatString = "{value:.0f}" else: self._drawIndicatorText_formatString = formatString