Description of the megawidget

This widget will implement a simple gauge which tracks an integer value supplied by a Scale widget, which selects a number from a range. The gauge indicates the setting as a percentage of the range. The completed megawidget will look like the one shown in figure 4.52.

The scale widget will be a component of the megawidget since the range may be set by the programmer; the size and color of the gauge may similarly be changed, as appropriate for the application, so we make this a component, too.

Figure 4.52 Gauge widget

4.4.2 Options

In addition to the options for the scale and gauge components, we will need to define some options for the megawidget. First, we define min and max to allow the programmer the range supported by the widget. Secondly, we define fill and size to control the color and size of the gauge. Lastly, we define value to allow us to set the initial value of the megawidget.

4.4.3 Creating the megawidget class

Pmw megawidgets inherit from either Pmw.MegaWidget, Pmw.MegaToplevel or Pmw.Dia-log. The gauge widget is intended to be used within other code widgets so it inherits from Pmw.MegaWidget. Here is the code for the megawidget. from Tkinter import * import Pmw class Gauge(Pmw.MegaWidget):

# Define the options for the megawidget optiondefs = ( (

min 1 ,






fill1 ,

1 red1,

None) ,




value 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',

Frame, (interior,), borderwidth=0) self.canvas = Canvas(self.gauge, width=self['size'], height=self['sizel], background=interior.cget('background')) self.canvas.pack(side=TOP, expand=1, fill=BOTH) 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:


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

def _setGauge(self, value):


arc = (360.0/ticks) * ival xy = 3,3,self['size'],self[lsizel]

start = 360 + start self.canvas.create_arc(xy, start=start, extent=arc-.001, fill=self['fill'], tags=('gauge',))

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




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

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

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


Code comments

1 Options for the megawidget are specified by a three-element sequence of the option name, default value and a final argument. The final argument can be either a callback function, Pmw.INITOPT or None. If it is Pmw.INITOPT then the option may only be provided as an initialization option and it cannot be set by calling configure. Calling self.defineop-tions includes keyword arguments passed in the widget's constructor. These values may override any default values.

2 Having set the options we call the constructor of the base class, passing the parent widget as the single argument.

3 By convention, Pmw defines an interior attribute which is the container for components.

4 We then create the gauge's indicator, which is going to be drawn on a canvas contained in a frame. The createcomponent method has five standard arguments (name, aliases, group, class and arguments to the constructor) followed by any number of keyword arguments.

5 Then, we construct the scale component in a similar manner.

6 Having completed the constructor, we first call initialiseoptions to check that all of the keyword arguments we supplied have been used. It then calls any option callbacks that have been defined.

7 Once the megawidget's class has been defined we call the Pmw.forwardmethods method to direct any method calls from other widgets to the scale component.

Arc Pygame
Figure 4.53 Using the gauge megawidget as a color mixer

Figure 4.53 illustrates a possible application of the gauge megawidget as a color mixer. The widget may be reconfigured to show or hide the current value of each slider. It is an easy task to add more options to the widget.

0 0

Post a comment