Skip to content

Commit

Permalink
Adds range field to wx.lib.agw.pygauge.PyGauge format string. (#2583)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sphh authored Nov 15, 2024
1 parent b102e88 commit 44fe243
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions wx/lib/agw/pygauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def __init__(self, parent):
"""

import wx
import copy


class PyGauge(wx.Window):
Expand Down Expand Up @@ -343,15 +342,15 @@ def OnPaint(self, event):
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:
for i, colour in enumerate(self._barColourSorted):
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)

Expand All @@ -364,7 +363,8 @@ def OnPaint(self, event):
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
Expand Down Expand Up @@ -392,9 +392,10 @@ def SetDrawValue(self, draw=True, drawPercent=True, font=None, colour=wx.BLACK,
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``.
Expand Down Expand Up @@ -423,7 +424,7 @@ def SetDrawValue(self, draw=True, drawPercent=True, font=None, colour=wx.BLACK,
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))
Expand All @@ -434,8 +435,8 @@ def SetDrawValue(self, draw=True, drawPercent=True, font=None, colour=wx.BLACK,
# 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

Expand Down

0 comments on commit 44fe243

Please sign in to comment.