OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python wx speedmeter working as clock

  • Thread starter Thread starter sansingh
  • Start date Start date
S

sansingh

Guest
I want to move hand of speedmeter (in 'wx' package) in Python with every second (same like clock) after click on a button. Action sequence would be like: 1. Click on a button 2. Hand of speedmeter starts moving after every 0.4 seconds. But after clicking on button (to start hand of speedmeter), color of frame goes dim (seems as app is crashed but actually there is no error or exception). I think it is happening because I am writing an infinite loop (to update hand of speedmeter) which is blocking APP.MainLoop() function. And I also think that thread programming can help me but I don't know much about thread programming. So, help me for this.

Following is my code:

Code:
import threading
import os
import wx.lib.agw.speedmeter as SM
import wx.lib.agw.speedmeter as SM2
import sys
import wx
import math
from wx import animate
import time
from random import randint
import thread

class MyFrame(wx.Frame):

    def __init__(self,parent,id,title):
        # creation frame
        l, h = wx.GetDisplaySize()
        wx.Frame.__init__(self,parent,-1,title,size=(l, h))

        self.speed = SM.SpeedMeter(self,agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS,pos=(15,10),size=(l/2,h/2))

        # Set The Region Of Existence Of SpeedMeter 1
        #self.speed.SetAngleRange(*-math.pi/4, math.pi/1.75)
        #self.speed.SetAngleRange(-2*math.pi/4, 2.57*math.pi/1.75)
        self.speed.SetAngleRange(-2*math.pi/4, 2.57*math.pi/1.75)

        # SpeedMeter In Sectors
        intervals = range(0, 5100, 100)
        self.speed.SetIntervals(intervals)

        # Assign The Same Colours To All Sectors
        colours = [wx.WHITE]*50
        self.speed.SetIntervalColours(colours)

        # Assign The Ticks
        ticks = [str(interval) for interval in intervals]
        self.speed.SetTicks(ticks)

        # Set The Ticks/Tick Markers Colour
        self.speed.SetTicksColour(wx.RED)

        # We Want To Draw 5 Secondary Ticks Between The Principal Ticks
        self.speed.SetNumberOfSecondaryTicks(4)

        # Set The Font For The Ticks Markers
        self.speed.SetTicksFont(wx.Font(8.9, wx.SWISS, wx.NORMAL, wx.NORMAL))

        # Set The Text In The Center Of SpeedMeter
        self.speed.SetMiddleText("Actual")

        # Assign The Colour To The Center Text
        self.speed.SetMiddleTextColour(wx.BLACK)

        # Assign A Font To The Center Text
        self.speed.SetMiddleTextFont(wx.Font(22, wx.SWISS, wx.NORMAL, wx.BOLD))

        # Set The Colour For The Hand Indicator
        self.speed.SetHandColour(wx.BLUE)#wx.Colour(255, 250, 20))

        # Do Not Draw The External (CONTAINER) Arc
        self.speed.DrawExternalArc(True)

        # adding button
        self.btn = wx.Button(self,2,"OK", pos = (400, 820), size = (50, 30))
        self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)

    def OnClicked(self, event):
        try:
            i = 0
            while(True):
                #r = randint(0, 5000)
                if(i >= 5000):
                    i = 0
                i += 50
                self.speed.SetSpeedValue(i)
                time.sleep(0.5)
        except Exception, err:
            print("Exception", Exception, err)

if __name__=='__main__' :
    APP=wx.App()
    frame=MyFrame(None,0,'Appsy')
    APP.SetTopWindow(frame)
    frame.Show()
    APP.MainLoop()
<p>I want to move hand of speedmeter (in 'wx' package) in Python with every second (same like clock) after click on a button. Action sequence would be like:
1. Click on a button
2. Hand of speedmeter starts moving after every 0.4 seconds.
But after clicking on button (to start hand of speedmeter), color of frame goes dim (seems as app is crashed but actually there is no error or exception). I think it is happening because I am writing an infinite loop (to update hand of speedmeter) which is blocking APP.MainLoop() function. And I also think that thread programming can help me but I don't know much about thread programming. So, help me for this.</p>

<p>Following is my code:</p>

<pre><code>import threading
import os
import wx.lib.agw.speedmeter as SM
import wx.lib.agw.speedmeter as SM2
import sys
import wx
import math
from wx import animate
import time
from random import randint
import thread

class MyFrame(wx.Frame):

def __init__(self,parent,id,title):
# creation frame
l, h = wx.GetDisplaySize()
wx.Frame.__init__(self,parent,-1,title,size=(l, h))

self.speed = SM.SpeedMeter(self,agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS,pos=(15,10),size=(l/2,h/2))

# Set The Region Of Existence Of SpeedMeter 1
#self.speed.SetAngleRange(*-math.pi/4, math.pi/1.75)
#self.speed.SetAngleRange(-2*math.pi/4, 2.57*math.pi/1.75)
self.speed.SetAngleRange(-2*math.pi/4, 2.57*math.pi/1.75)

# SpeedMeter In Sectors
intervals = range(0, 5100, 100)
self.speed.SetIntervals(intervals)

# Assign The Same Colours To All Sectors
colours = [wx.WHITE]*50
self.speed.SetIntervalColours(colours)

# Assign The Ticks
ticks = [str(interval) for interval in intervals]
self.speed.SetTicks(ticks)

# Set The Ticks/Tick Markers Colour
self.speed.SetTicksColour(wx.RED)

# We Want To Draw 5 Secondary Ticks Between The Principal Ticks
self.speed.SetNumberOfSecondaryTicks(4)

# Set The Font For The Ticks Markers
self.speed.SetTicksFont(wx.Font(8.9, wx.SWISS, wx.NORMAL, wx.NORMAL))

# Set The Text In The Center Of SpeedMeter
self.speed.SetMiddleText("Actual")

# Assign The Colour To The Center Text
self.speed.SetMiddleTextColour(wx.BLACK)

# Assign A Font To The Center Text
self.speed.SetMiddleTextFont(wx.Font(22, wx.SWISS, wx.NORMAL, wx.BOLD))

# Set The Colour For The Hand Indicator
self.speed.SetHandColour(wx.BLUE)#wx.Colour(255, 250, 20))

# Do Not Draw The External (CONTAINER) Arc
self.speed.DrawExternalArc(True)

# adding button
self.btn = wx.Button(self,2,"OK", pos = (400, 820), size = (50, 30))
self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)

def OnClicked(self, event):
try:
i = 0
while(True):
#r = randint(0, 5000)
if(i >= 5000):
i = 0
i += 50
self.speed.SetSpeedValue(i)
time.sleep(0.5)
except Exception, err:
print("Exception", Exception, err)

if __name__=='__main__' :
APP=wx.App()
frame=MyFrame(None,0,'Appsy')
APP.SetTopWindow(frame)
frame.Show()
APP.MainLoop()
</code></pre>
 
Top