Gauge made from Pmw.MegaWidget : Pmw Gauge « GUI Pmw « Python






Gauge made from Pmw.MegaWidget

Gauge made from Pmw.MegaWidget
#Pmw copyright

#Copyright 1997-1999 Telstra Corporation Limited, Australia 
#Copyright 2000-2002 Really Good Software Pty Ltd, Australia

#Permission is hereby granted, free of charge, to any person obtaining a copy 
#of this software and associated documentation files (the "Software"), to deal 
#in the Software without restriction, including without limitation the rights 
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
#copies of the Software, and to permit persons to whom the Software is furnished 
#to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in all 
#copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 





from Tkinter import *
import Pmw

class Gauge(Pmw.MegaWidget):
    def __init__(self, parent=None, **kw):

        # Define the options for the megawidget
        optiondefs = (
            ('min',           0,          Pmw.INITOPT),
            ('max',           100,        Pmw.INITOPT),
            ('fill',          'red',      None),
            ('size',          30,         Pmw.INITOPT),
            ('value',         0,          None),
            ('showvalue',     1,          None),
        )
        self.defineoptions(kw, optiondefs)

        # Initialize the base class
        Pmw.MegaWidget.__init__(self, parent)

        interior = self.interior()

        # Create the gauge component
        self.gauge = self.createcomponent('gauge',
                             (), None,
                             Frame, (interior,),
                             borderwidth=0)
        self.canvas = Canvas(self.gauge,
                             width=self['size'], height=self['size'],
                             background=interior.cget('background'))
        self.canvas.pack(side=TOP, expand=1, fill=BOTH, anchor=CENTER)
        self.gauge.grid()

        # Create the scale component
        self.scale = self.createcomponent('scale',
                             (), None,
                             Scale, (interior,),
                             command=self._setGauge,
                             length=200,
                             from_ = self['min'],
                             to    = self['max'],
                             showvalue=self['showvalue'])
        self.scale.grid()

        value=self['value']
        if value is not None:
            self.scale.set(value)

        # Check keywords and initialize options
        self.initialiseoptions(Gauge)

    def _setGauge(self, value):
        self.canvas.delete('gauge')
        ival = self.scale.get()
        ticks = self['max'] - self['min']
        arc = (360.0/ticks) * ival
        xy = 3,3,self['size'],self['size']
        start = 90-arc
        if start < 0:
            start = 360 + start
        self.canvas.create_arc(xy, start=start, extent=arc-.001,
                               fill=self['fill'], tags=('gauge',))

Pmw.forwardmethods(Gauge, Scale, 'scale')

root = Tk()
#root.option_readfile('optionDB')
root.title('Gauge')
Pmw.initialise()

g1 = Gauge(root, fill='red', value=10, min=0, max=255)
g1.pack(side=LEFT, padx=1, pady=10)

g2 = Gauge(root, fill='green', value=20, min=0, max=255)
g2.pack(side=LEFT, padx=1, pady=10)

g3 = Gauge(root, fill='blue', value=30,  min=0, max=255)
g3.pack(side=LEFT, padx=1, pady=10)

g4 = Gauge(root, fill='black', value=40,  min=0, max=255)
g4.pack(side=LEFT, padx=1, pady=10)

g5 = Gauge(root, fill='pink', value=50,  min=0, max=255)
g5.pack(side=LEFT, padx=1, pady=10)

g6 = Gauge(root, fill='yellow', value=60,  min=0, max=255)
g6.pack(side=LEFT, padx=1, pady=10)

g7 = Gauge(root, fill='purple', value=70,  min=0, max=255)
g7.pack(side=LEFT, padx=1, pady=10)

g8 = Gauge(root, fill='gray', value=80,  min=0, max=255)
g8.pack(side=LEFT, padx=1, pady=10)


root.mainloop()


           
       








Related examples in the same category