[Tutor] Multiple inheritance

Phil phillor9 at gmail.com
Fri May 14 00:53:48 EDT 2021


Thank you for reading this.

This question relates to multiple inheritance rather than a question 
about wxPython. The code actually works, or did until I tried to create 
a separate meter class . The problem, as I see it, is that the MyForm 
class is not inheriting the Meter class and there are bound to be other 
errors as well. I'm sorry to burden the list with this much code.

import wx
import math

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

class MyForm(wx.Frame, Meter):
     def __init__(self):
         wx.Frame.__init__(self, None, wx.ID_ANY, "Meter Test", 
size=(300, 300))

         # This results in a syntax error
         Meter.__init__(self, start_x)
         # I also tried super

         '''
         These variables have been moved to the meter class

         self.angle = 180  # start with the pointer pointing to the left
         self.rad = 0
         self.start_x = 200  # the starting point of the pointer
         self.start_y = 200
         self.length = 50  # the length of the pointer
         self.inc_amount = 5
         '''
         self.InitUI()

     def InitUI(self):

         self.Bind(wx.EVT_PAINT, self.OnPaint)

         panel = wx.Panel(self, wx.ID_ANY)

         self.timer = wx.Timer(self)
         self.Bind(wx.EVT_TIMER, self.update, self.timer)
         self.timer.Start(100)

     def update(self, event):
         m = Meter(200) # m.self.angle etc is not correct here. 
Shouldn't the Meter class variables

                                     #  be inherited and be useable with 
the need for m.self?

         if self.angle == 360:
             self.inc_amount *= -1

         self.angle += self.inc_amount

         if self.angle == 180:
             self.inc_amount *= -1

         self.rad = (self.angle * math.pi / 180)

         self.Refresh()

     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(self.start_x, self.start_y, 60, math.radians(180),
                         math.radians(0), 1)
             path.AddLineToPoint(140, self.start_y)
             #gc.StrokePath(path)
             gc.DrawPath(path)

             x = int(self.start_x + math.cos(self.rad) * self.length)
             y = int(self.start_y + math.sin(self.rad) * self.length)

             dc.DrawLine(self.start_x, self.start_y, x, y)


# Run the program
if __name__ == "__main__":
     app = wx.App()
     frame = MyForm().Show()
     app.MainLoop()

-- 

Regards,
Phil



More information about the Tutor mailing list