SpeedMeter tries to reproduce the behavior of some car controls (but not only), by creating an “angular” control (actually, circular).
SpeedMeter tries to reproduce the behavior of some car controls (but not only), by creating an “angular” control (actually, circular). I remember to have seen it somewhere, and i decided to implement it in wxPython.
SpeedMeter starts its construction from an empty bitmap, and it uses some functions of the wx.DC class to create the rounded effects. everything is processed in the Draw() method of SpeedMeter class.
This implementation allows you to use either directly the wx.PaintDC, or the better (for me) double buffered style with wx.BufferedPaintDC. the double buffered implementation has been adapted from the wxPython wiki example:
http://wiki.wxpython.org/index.cgi/doublebuffereddrawing
Usage example:
import wx
import wx.lib.agw.speedmeter as SM
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "SpeedMeter Demo")
speed = SM.SpeedMeter(self, agwStyle=SM.SM_DRAW_HAND|SM.SM_DRAW_SECTORS|SM.SM_DRAW_MIDDLE_TEXT|SM.SM_DRAW_SECONDARY_TICKS)
# Set The Region Of Existence Of SpeedMeter (Always In Radians!!!!)
speed.SetAngleRange(-pi/6, 7*pi/6)
# Create The Intervals That Will Divide Our SpeedMeter In Sectors
intervals = range(0, 201, 20)
speed.SetIntervals(intervals)
# Assign The Same Colours To All Sectors (We Simulate A Car Control For Speed)
# Usually This Is Black
colours = [wx.BLACK]*10
speed.SetIntervalColours(colours)
# Assign The Ticks: Here They Are Simply The String Equivalent Of The Intervals
ticks = [str(interval) for interval in intervals]
speed.SetTicks(ticks)
# Set The Ticks/Tick Markers Colour
speed.SetTicksColour(wx.WHITE)
# We Want To Draw 5 Secondary Ticks Between The Principal Ticks
speed.SetNumberOfSecondaryTicks(5)
# Set The Font For The Ticks Markers
speed.SetTicksFont(wx.Font(7, wx.SWISS, wx.NORMAL, wx.NORMAL))
# Set The Text In The Center Of SpeedMeter
speed.SetMiddleText("Km/h")
# Assign The Colour To The Center Text
speed.SetMiddleTextColour(wx.WHITE)
# Assign A Font To The Center Text
speed.SetMiddleTextFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
# Set The Colour For The Hand Indicator
speed.SetHandColour(wx.Colour(255, 50, 0))
# Do Not Draw The External (Container) Arc. Drawing The External Arc May
# Sometimes Create Uglier Controls. Try To Comment This Line And See It
# For Yourself!
speed.DrawExternalArc(False)
# Set The Current Value For The SpeedMeter
speed.SetSpeedValue(44)
# our normal wxApp-derived class, as usual
app = wx.PySimpleApp()
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
This code snippet can be downloaded, see this example script.
Note
Some of the AGW snippets of code in the documentation use images and external files (to create bitmaps or access external data). As these files are not provided in these snippets, you should make the approriate modifications to the code to actually run it.
SpeedMeter is highly customizable, and in particular you can set:
This class supports the following window styles:
Window Styles | Hex Value | Description |
---|---|---|
SM_ROTATE_TEXT | 0x1 | Draws the ticks rotated: the ticks are rotated accordingly to the tick marks positions. |
SM_DRAW_SECTORS | 0x2 | Different intervals are painted in differend colours (every sector of the circle has its own colour). |
SM_DRAW_PARTIAL_SECTORS | 0x4 | Every interval has its own colour, but only a circle corona is painted near the ticks. |
SM_DRAW_HAND | 0x8 | The hand (arrow indicator) is drawn. |
SM_DRAW_SHADOW | 0x10 | A shadow for the hand is drawn. |
SM_DRAW_PARTIAL_FILLER | 0x20 | A circle corona that follows the hand position is drawn near the ticks. |
SM_DRAW_SECONDARY_TICKS | 0x40 | Intermediate (smaller) ticks are drawn between principal ticks. |
SM_DRAW_MIDDLE_TEXT | 0x80 | Some text is printed in the middle of the control near the center. |
SM_DRAW_MIDDLE_ICON | 0x100 | An icon is drawn in the middle of the control near the center. |
SM_DRAW_GRADIENT | 0x200 | A gradient of colours will fill the control. |
SM_DRAW_FANCY_TICKS | 0x400 | With this style you can use xml tags to create some custom text and draw it at the ticks position. See wx.lib.fancytext for the tags. |
SpeedMeter is distributed under the wxPython license.
Latest revision: Andrea Gavana @ 17 Aug 2011, 15.00 GMT
Version 0.3
Module author: Andrea Gavana <andrea.gavana@gmail.com>
A graphical representation of the SVN commits in the last year.
Click on any date in the picture to jump to that particular revision page, containing information about committers, log messages and SVN diffs.