Search code examples
python-3.xwxpython

PyGauge (wxPython Phoenyx) does not expand with Frame


I wanted to add a couple or marks into a wx.pyGauge to indicate min, max custom limits.
This is the code:

import wx
from wx.lib.agw.pygauge import PyGauge as PG
#
#
class AFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.gauge = GaugeBar(self)
        p1 = wx.Panel(self)
        p2 = wx.Panel(self)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(p1, 1, wx.EXPAND)
        self.sizer.Add(self.gauge, 0, wx.EXPAND)
        self.sizer.Add(p2, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        self.Fit()
        self.SetSize((400, 100))


class GaugeBar(PG):
    def __init__(self, parent, size=(-1, 20)):
        self.range_width = 14
        self.limit_min, self.limit_max = (3, 8)
        #
        PG.__init__(self, parent, -1, self.range_width, size, style=wx.GA_HORIZONTAL)
        #
        self.SetBackgroundColour('yellow')
        self.SetForegroundColour('red')
        #
        self.Bind(wx.EVT_PAINT, self.onPaint)
    #
    def onPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.Clear()
        PG.OnPaint(self, evt)
        #
        w, h = dc.GetSize()
        #
        xmin = self.limit_min * w / self.range_width
        xmax = self.limit_max * w / self.range_width
        #
        dc.DrawText('|', xmin, -8)
        dc.DrawText('|', xmin, 10)
        dc.DrawText('|', xmax, -8)
        dc.DrawText('|', xmax, 10)

#
if __name__ == '__main__':

    app = wx.App()
    a_frame = AFrame(None)
    a_frame.gauge.SetValue(10)
    a_frame.Show()
    app.MainLoop()

The gauge appears correctly with the two red marks and the blue gauge level.

However, when I extend horizontally the frame, neither the gauge level indicator or the red marks are refreshed to reflect the new proportions.

Interestingly, if I close and extend vertically the frame, it gets refreshed with the gauge and marks in the correct positions.

enter image description here

What I am doing wrong ? Is this a bug ?


Solution

  • On Windows, by default, only the newly exposed areas of a window are painted on resizes and the rest is clipped out of the update region. So although your onPaint is being called, the existing parts of the window are excluded from the drawing region. The easiest way to avoid this is to use the wx.FULL_REPAINT_ON_RESIZE style when creating the widget, however you may then need to deal with some extra flicker in some cases.