0
\$\begingroup\$

I need to create a GUI using WxPython in the form of a Speedometer to display 9 different data values fetched from Adafruit IO. My code is shown below:

import wx
import wx.lib.agw.speedmeter as SM
import time
from math import pi
import numpy as np
from Adafruit_IO import Client

aio = Client(NAME, PASSWORD)

global ap1
global ap2
global ap3
global lc1
global lc2
global lc3
global lv1
global lv2
global lv3

ap1 = 0
ap2 = 0
ap3 = 0
lc1 = 0
lc2 = 0
lc3 = 0
lv1 = 0
lv2 = 0
lv3 = 0

class PanelOne(wx.Panel):
    def __init__(self, parent):
        global ap1
        global ap2
        global ap3
        global lc1
        global lc2
        global lc3
        global lv1
        global lv2
        global lv3
        wx.Panel.__init__(self, parent=parent)
        panel = wx.Panel(self, wx.ID_ANY)
        panel1 = wx.Panel(panel, wx.ID_ANY, style=wx.SUNKEN_BORDER)

        self.speed1 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed2 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed3 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed4 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed5 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed6 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed7 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed8 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)
        self.speed9 = SM.SpeedMeter(panel1, -1, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS, size = (200,200), mousestyle=1)

        self.speed1.SetAngleRange(-pi/6, 7*pi/6)
        self.speed2.SetAngleRange(-pi/6, 7*pi/6)
        self.speed3.SetAngleRange(-pi/6, 7*pi/6)
        self.speed4.SetAngleRange(-pi/6, 7*pi/6)
        self.speed5.SetAngleRange(-pi/6, 7*pi/6)
        self.speed6.SetAngleRange(-pi/6, 7*pi/6)
        self.speed7.SetAngleRange(-pi/6, 7*pi/6)
        self.speed8.SetAngleRange(-pi/6, 7*pi/6)
        self.speed9.SetAngleRange(-pi/6, 7*pi/6)

        interval1 = [0, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001]
        interval2 = [0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1]
        interval3 = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
        interval4 = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300]
        interval5 = [0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009, 0.001]

        self.speed1.SetIntervals(interval1)
        self.speed2.SetIntervals(interval1)
        self.speed3.SetIntervals(interval2)
        self.speed4.SetIntervals(interval1)
        self.speed5.SetIntervals(interval5)
        self.speed6.SetIntervals(interval3)
        self.speed7.SetIntervals(interval4)
        self.speed8.SetIntervals(interval4)
        self.speed9.SetIntervals(interval4)

        colour1 = [wx.BLACK]*10

        self.speed1.SetIntervalColours(colour1)
        self.speed2.SetIntervalColours(colour1)
        self.speed3.SetIntervalColours(colour1)
        self.speed4.SetIntervalColours(colour1)
        self.speed5.SetIntervalColours(colour1)
        self.speed6.SetIntervalColours(colour1)
        self.speed7.SetIntervalColours(colour1)
        self.speed8.SetIntervalColours(colour1)
        self.speed9.SetIntervalColours(colour1)

        tick1 = [str(interval) for interval in interval1]
        tick2 = [str(interval) for interval in interval2]
        tick3 = [str(interval) for interval in interval3]
        tick4 = [str(interval) for interval in interval4]
        tick5 = [str(interval) for interval in interval5]

        self.speed1.SetTicks(tick1)
        self.speed1.SetTicksColour(wx.WHITE)
        self.speed1.SetNumberOfSecondaryTicks(5)
        self.speed1.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed1.SetMiddleText("Active Power 1")
        self.speed1.SetMiddleTextColour(wx.WHITE)
        self.speed1.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed1.SetHandColour(wx.Colour(255, 50, 0))
        self.speed1.DrawExternalArc(False)
        self.speed1.SetSpeedValue(0)

        self.speed2.SetTicks(tick1)
        self.speed2.SetTicksColour(wx.WHITE)
        self.speed2.SetNumberOfSecondaryTicks(5)
        self.speed2.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed2.SetMiddleText("Active Power 2")
        self.speed2.SetMiddleTextColour(wx.WHITE)
        self.speed2.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed2.SetHandColour(wx.Colour(255, 50, 0))
        self.speed2.DrawExternalArc(False)
        self.speed2.SetSpeedValue(0)

        self.speed3.SetTicks(tick2)
        self.speed3.SetTicksColour(wx.WHITE)
        self.speed3.SetNumberOfSecondaryTicks(5)
        self.speed3.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed3.SetMiddleText("Active Power 3")
        self.speed3.SetMiddleTextColour(wx.WHITE)
        self.speed3.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed3.SetHandColour(wx.Colour(255, 50, 0))
        self.speed3.DrawExternalArc(False)
        self.speed3.SetSpeedValue(0)

        self.speed4.SetTicks(tick1)
        self.speed4.SetTicksColour(wx.WHITE)
        self.speed4.SetNumberOfSecondaryTicks(5)
        self.speed4.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed4.SetMiddleText("Line Current 1")
        self.speed4.SetMiddleTextColour(wx.WHITE)
        self.speed4.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed4.SetHandColour(wx.Colour(255, 50, 0))
        self.speed4.DrawExternalArc(False)
        self.speed4.SetSpeedValue(0)

        self.speed5.SetTicks(tick5)
        self.speed5.SetTicksColour(wx.WHITE)
        self.speed5.SetNumberOfSecondaryTicks(5)
        self.speed5.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed5.SetMiddleText("Line Current 2")
        self.speed5.SetMiddleTextColour(wx.WHITE)
        self.speed5.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed5.SetHandColour(wx.Colour(255, 50, 0))
        self.speed5.DrawExternalArc(False)
        self.speed5.SetSpeedValue(0)

        self.speed6.SetTicks(tick2)
        self.speed6.SetTicksColour(wx.WHITE)
        self.speed6.SetNumberOfSecondaryTicks(5)
        self.speed6.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed6.SetMiddleText("Line Current 3")
        self.speed6.SetMiddleTextColour(wx.WHITE)
        self.speed6.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed6.SetHandColour(wx.Colour(255, 50, 0))
        self.speed6.DrawExternalArc(False)
        self.speed6.SetSpeedValue(0)

        self.speed7.SetTicks(tick4)
        self.speed7.SetTicksColour(wx.WHITE)
        self.speed7.SetNumberOfSecondaryTicks(5)
        self.speed7.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed7.SetMiddleText("Line Voltage 1")
        self.speed7.SetMiddleTextColour(wx.WHITE)
        self.speed7.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed7.SetHandColour(wx.Colour(255, 50, 0))
        self.speed7.DrawExternalArc(False)
        self.speed7.SetSpeedValue(0)

        self.speed8.SetTicks(tick4)
        self.speed8.SetTicksColour(wx.WHITE)
        self.speed8.SetNumberOfSecondaryTicks(5)
        self.speed8.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed8.SetMiddleText("Line Voltage 2")
        self.speed8.SetMiddleTextColour(wx.WHITE)
        self.speed8.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed8.SetHandColour(wx.Colour(255, 50, 0))
        self.speed8.DrawExternalArc(False)
        self.speed8.SetSpeedValue(0)

        self.speed9.SetTicks(tick4)
        self.speed9.SetTicksColour(wx.WHITE)
        self.speed9.SetNumberOfSecondaryTicks(5)
        self.speed9.SetTicksFont(wx.Font(7, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        self.speed9.SetMiddleText("Line Voltage 3")
        self.speed9.SetMiddleTextColour(wx.WHITE)
        self.speed9.SetMiddleTextFont(wx.Font(8, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        self.speed9.SetHandColour(wx.Colour(255, 50, 0))
        self.speed9.DrawExternalArc(False)
        self.speed9.SetSpeedValue(0)

        vsizer1 = wx.BoxSizer(wx.VERTICAL)
        hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer3 = wx.BoxSizer(wx.HORIZONTAL)

        hsizer1.Add(self.speed1, 0, wx.EXPAND)
        hsizer1.Add(self.speed2, 0, wx.EXPAND)
        hsizer1.Add(self.speed3, 0, wx.EXPAND)
        vsizer1.Add(hsizer1, 0, wx.EXPAND)
        hsizer2.Add(self.speed4, 0, wx.EXPAND)
        hsizer2.Add(self.speed5, 0, wx.EXPAND)
        hsizer2.Add(self.speed6, 0, wx.EXPAND)
        vsizer1.Add(hsizer2, 0, wx.EXPAND)
        hsizer3.Add(self.speed7, 0, wx.EXPAND)
        hsizer3.Add(self.speed8, 0, wx.EXPAND)
        hsizer3.Add(self.speed9, 0, wx.EXPAND)
        vsizer1.Add(hsizer3, 0, wx.EXPAND)

        panel1.SetSizer(vsizer1)
        panel1.Fit()
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(mainSizer)
        mainSizer.Layout()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1)
        self.Show(True)

    def on_timer(self,event):
        global ap1
        global ap2
        global ap3
        global lc1
        global lc2
        global lc3
        global lv1
        global lv2
        global lv3

        ap1 = float((aio.receive('activepower-kt1')).value)
        ap2 = float((aio.receive('activepower-kt2')).value)
        ap3 = float((aio.receive('activepower-kt3')).value)
        lc1 = float((aio.receive('linecurrent-i1')).value)
        lc2 = float((aio.receive('linecurrent-i2')).value)
        lc3 = float((aio.receive('linecurrent-i3')).value)
        lv1 = float((aio.receive('linevoltage-v1')).value)
        lv2 = float((aio.receive('linevoltage-v2')).value)
        lv3 = float((aio.receive('linevoltage-v3')).value)

        self.speed1.SetSpeedValue(ap1)
        self.speed2.SetSpeedValue(ap2)
        self.speed3.SetSpeedValue(ap3)
        self.speed4.SetSpeedValue(lc1)
        self.speed5.SetSpeedValue(lc2)
        self.speed6.SetSpeedValue(lc3)
        self.speed7.SetSpeedValue(lv1)
        self.speed8.SetSpeedValue(lv2)
        self.speed9.SetSpeedValue(lv3)

class MyApp(wx.App):
    def OnInit(self):
        title = "Energy Meter GUI"
        pos = (200,200)
        size =(570,280)
        self.frame = class_myframe(title, pos , size)
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        return True

class class_myframe(wx.Frame):
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self, None, wx.ID_ANY, title, pos, size)
        self.panel_one = PanelOne(self)
        self.boxsizer = wx.BoxSizer(wx.VERTICAL)
        self.boxsizer.Add(self.panel_one, 1, wx.EXPAND)
        self.SetSizer(self.boxsizer)
        self.boxsizer.Fit(self)
        self.boxsizer.Layout()

app = MyApp(False)
app.MainLoop() 

This code does what it is meant to do nicely. However, it hangs quite often and then updates the values after a while. Is there any way I can optimize it further?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.