I needed a simple gauge, so I've made on with Pmw.
It might be useful for others to use as a base to develop more comples
Is it worth cleaning up and submitting?
if sys
.platform
== 'win32':
# MS-Windows specific fonts
label_font
= "-family Ariel -size 12"
value_font
= "-family Ariel -size 12"
small_font
= "-family {MS Sans Serif} -size 9 -weight bold"
header_font
= "-family {MS Sans Serif} -weight bold"
# X-Windows specific fonts
label_font
= "-*-helvetica-*-r-*-*-*-160-*-*-*-*-*-*"
value_font
= "-*-courier-*-r-*-*-*-160-*-*-*-*-*-*"
small_font
= "-*-helvetica-*-r-*-*-*-130-*-*-*-*-*-*"
header_font
= "-*-helvetica-bold-r-*-*-*-150-*-*-*-*-*-*"
class VerticalGauge(Pmw
.MegaWidget
):
"""Vertical gauge with actual and desired settings"""
def __init__(self
, parent
= None, **kw
):
('majortickinterval', 10, None),
('minortickinterval', 5, None),
('bg', 'grey', self
._backgroundSet
),
('actualvalue', 50, self
._actualSet
),
('desiredvalue', 50, self
._desiredSet
),
('actualcolour', 'yellow1', None),
('desiredcolour', 'turquoise1', None),
('label', 'Label', None),
self
.defineoptions(kw
, optiondefs
)
Pmw
.MegaWidget
.__init
__(self
, parent
)
interior
= self
.interior()
interior
.grid_rowconfigure(1, weight
= 1)
interior
.grid_columnconfigure(r
, weight
= 1)
self
.actuallabel
= self
.createcomponent('actualLabel',
Tkinter
.Label
, (interior
,),
fg
= self
['actualcolour'],
self
.actuallabel
.grid(sticky
= "nswe", row
= 0, column
= 0)
self
.label
= self
.createcomponent('label',
Tkinter
.Label
, (interior
,),
self
.label
.grid(sticky
= "nsew", row
= 0, column
= 1)
self
.desiredlabel
= self
.createcomponent('desiredLabel',
Tkinter
.Label
, (interior
,),
fg
= self
['desiredcolour'],
self
.desiredlabel
.grid(sticky
= "nswe", row
= 0, column
= 2)
self
.canvas
= self
.createcomponent('canvas',
Tkinter
.Canvas
, (interior
,),
self
.canvas
.grid(sticky
= "nsew", columnspan
= 3, pady
= 1)
self
.canvas
.bind("<Configure>", self
._createGaugeAxes
)
def _createGaugeAxes(self
, event
= None):
majortickinterval
= self
['majortickinterval']
h
, w
= event
.height
, event
.width
h
= int(c
.configure("height")[4])
w
= int(c
.configure("width")[4])
c
.create_line(self
.middle
, self
.lower
, self
.middle
, self
.upper
)
majortickcount
= int((max - min) / majortickinterval
)
self
.axislength
= self
.lower
- self
.upper
self
.majortickdistance
= float(self
.axislength
) / majortickcount
self
.majortickwidth
= w
/ 5
labeloffset
= (w
/ 4) + 10
for i
in range(majortickcount
+ 1):
v
= min + i
* majortickinterval
d
= self
.lower
- i
* self
.majortickdistance
c
.create_line(self
.middle
, d
, self
.middle
+ self
.majortickwidth
, d
)
c
.create_text(self
.middle
+ labeloffset
, d
, font
= small_font
, text
= str(v
))
def _backgroundSet(self
):
self
.canvas
.configure(bg
= self
['bg'])
def _desiredSet(self
, event
= None):
desired
= self
['desiredvalue']
desiredcolour
= self
['desiredcolour']
if desired
> max: desired
= max
if desired
< min: desired
= min
h
, w
= event
.height
, event
.width
h
= int(c
.configure("height")[4])
w
= int(c
.configure("width")[4])
desired_y
= self
.lower
- (float(desired
- min) / gauge_range
) * self
.axislength
c
.create_line(self
.middle
- self
.majortickwidth
, desired_y
,
self
.middle
+ self
.majortickwidth
, desired_y
,
fill
= desiredcolour
, stipple
= 'gray50',
width
= 10, tag
= 'desiredBar')
self
.desiredlabel
.configure(text
= desired
)
def setActual(self
, value
):
self
.configure(actualvalue
= value
)
return self
.cget('actualvalue')
def _actualSet(self
, event
= None):
actual
= self
['actualvalue']
actualcolour
= self
['actualcolour']
if actual
> max: actual
= max
if actual
< min: actual
= min
h
, w
= event
.height
, event
.width
h
= int(c
.configure("height")[4])
w
= int(c
.configure("width")[4])
actual_y
= self
.lower
- (float(actual
- min) / gauge_range
) * self
.axislength
c
.delete('actualPointer')
triangle
= ((self
.middle
, actual_y
),
(self
.middle
- 1.4 * self
.majortickwidth
, actual_y
- self
.majortickwidth
/ 2),
(self
.middle
- 1.4 * self
.majortickwidth
, actual_y
+ self
.majortickwidth
/ 2))
c
.create_polygon(triangle
, fill
= actualcolour
, tag
= 'actualPointer')
self
.actuallabel
.configure(text
= actual
)
Pmw
.forwardmethods(VerticalGauge
, Tkinter
.Canvas
, 'canvas')
if __name__
== '__main__':
# Initialise Tkinter and Pmw.
root
.title('Pmw VerticalGauge demonstration')
g1
= VerticalGauge(min = 0,
root
.grid_rowconfigure(0, weight
= 1)
root
.grid_columnconfigure(0, weight
= 1)
b1
= Tkinter
.Button(text
= "Increase", command
= increase
)
b2
= Tkinter
.Button(text
= "Decrease", command
= decrease
)