[Tutor] Multiple inheritance

Alan Gauld alan.gauld at yahoo.co.uk
Fri May 14 08:18:53 EDT 2021


On 14/05/2021 11:58, Phil wrote:

> Thank you Alan, I think this is what's required. Still, m = Meter(200) 
> and then print(m.angle) doesn't generate an error but doesn't print 
> anything

It should do, it should print 180.
It works for me...

>>> class Meter:
      def __init__(self, start_x):
          self.angle = 180  # start with the pointer pointing to the left
          self.rad = 0
          self.start_x = start_x  # the starting point of the pointer
          self.start_y = 200
          self.length = 50  # the length of the pointer
          self.inc_amount = 5


>>> m = Meter(200)
>>> print(m.angle)
180


> Also, how do I tell the MyForm class 
> methods (update() and OnPaint()) that the variables angle etc belong to 
> the Meter class? 

class MyForm(wx.Frame):
   def __init__(self, aMeter,....):
      wx.Frame.__init(....)
      self.meter = aMeter
      .... as before

[Aside: You will often see people referring to Model/View architecture
in connection with GUI projects(and even Web systems). This is an
example of a Model/View relationship. The Meter is a model of
something in the problem domain and your form is a GUI view
of that model. The view is separate from the model but tightly
coupled to it. Problem domain logic and rules should be built as
methods of the Meter class, representation features should be
in the view class. This makes it easier to move the model to
a different application - possibly web based - in the future.
You only need to create a new view class for the new UI.)


def update(self):
   if self.meter.angle == 360:....

>      def update(self, event):
>          if m.angle == 360:

Note that if you do use MI rather than containment you would
write this as

      def update(self, event):
          if self.angle == 360:

The inherited Meter attributes are part of the Form class
they are not in any separate m instance. Just as the genes
you inherited from your parents are actually part of you not
something separate.

>              Meter.inc_amount *= -1

So this becomes 9using cotainment)

self.meter.inc_amount *= -1

and using MI:

self.inc_amount *= -1

>      def OnPaint(self, e):
>          dc = wx.PaintDC(self)
> 
>          # Create graphics context
>          gc = wx.GraphicsContext.Create(dc)
> 
>          if gc:
>              gc.SetPen(wx.RED_PEN)
>              path = gc.CreatePath()
> 
>              path.AddArc(Meter.start_x, 
                           Meter.start_y, 60,
 math.radians(180),                           math.radians(0), 1)


Becomes
>              path.AddArc(self.meter.start_x,
                           self.meter.start_y, 60,
                           math.radians(180),
                           math.radians(0), 1)

or with MI:

>              path.AddArc(self.start_x,
                           self.start_y, 60,
                           math.radians(180),
                           math.radians(0), 1)


etc...
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list