How do I access variables and methods from a custom class?
Phil <[email protected]> Sun, 16 May 2021 15:58:29 +1000
| Newsgroups | gmane.comp.python.wxpython |
|---|---|
| Message-ID | <[email protected]> |
Thank you for reading this and I'm sorry for the length of this question. Continuing from my previous question, I'm trying to model an analogue meter as a reusable class that I can then import into in a viewer programme. The following code basically works but I don't think the method of accessing the variables from the Meter class is correct. Also, the connection between the Meter class and the MyForm class seems to be too tight, if that's the correct term. Finally, I think there's a need for a general method in my Meter class if I want to implement two meters in the MyForm class. I've given this a lot of thought but the best that I have so far involves a lot of duplication which I'm sure is not the way to go about this. import wx import math class Meter: def __init__(self, start_x, start_y, meter_size): self.angle = 180 # start with the needle pointing to the left self.rads = 0 self.start_x = start_x # the starting point of the needle self.start_y = start_y self.meter_size = meter_size self.needle_length = int((self.meter_size / 100) * 90) self.inc_amount = 5 # for sweep testing class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Meter Test", size=(300, 300)) self.meter = Meter(200, 200, 60) 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): # sweep the needle for testing only if self.meter.angle == 360: self.meter.inc_amount *= -1 self.meter.angle += self.meter.inc_amount if self.meter.angle == 180: self.meter.inc_amount *= -1 # convert angle to rads self.meter.rads = (self.meter.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.meter.start_x, self.meter.start_y, self.meter.meter_size, math.radians(180), math.radians(0), 1) path.AddLineToPoint(140, self.meter.start_y) #gc.StrokePath(path) gc.DrawPath(path) # Draw the needle x = int(self.meter.start_x + math.cos(self.meter.rads) * self.meter.needle_length) y = int(self.meter.start_y + math.sin(self.meter.rads) * self.meter.needle_length) dc.DrawLine(self.meter.start_x, self.meter.start_y, x, y) if __name__ == "__main__": app = wx.App() frame = MyForm().Show() app.MainLoop() -- Regards, Phil -- You received this message because you are subscribed to the Google Groups "wxPython-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/3e8c972f-efbe-1af7-c59f-c8799b6ecae2%40gmail.com.