WxPython GUI to display data from Adafruit IO
$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?
python-3.x gui wxpython
New contributor
$endgroup$
add a comment |
$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?
python-3.x gui wxpython
New contributor
$endgroup$
add a comment |
$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?
python-3.x gui wxpython
New contributor
$endgroup$
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?
python-3.x gui wxpython
python-3.x gui wxpython
New contributor
New contributor
New contributor
asked 6 mins ago
Parth Sarthi SharmaParth Sarthi Sharma
11
11
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Parth Sarthi Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211799%2fwxpython-gui-to-display-data-from-adafruit-io%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Parth Sarthi Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Parth Sarthi Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Parth Sarthi Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Parth Sarthi Sharma is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211799%2fwxpython-gui-to-display-data-from-adafruit-io%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown