| PyGTK Tutorial | ||
|---|---|---|
| <<< Previous | Chapter 9. Miscellaneous Widgets | Next >>> |
A ruler must first be created. Horizontal
and vertical rulers are created using
hruler = GtkHRuler() # horizontal ruler vruler = GtkVRuler() # vertical ruler |
Once a ruler is created, we can define
the unit of measurement. Units of measure for rulers can be PIXELS,
INCHES
or
CENTIMETERS. This is set using the method:
ruler.set_metric(metric) |
The default measure is PIXELS.
ruler.set_metric(PIXELS) |
Other important characteristics of a ruler
are how to mark the units of scale and where the position indicator is
initially placed. These are set for a ruler using the method:
ruler.set_range(lower, upper, position, max_size) |
The lower and upper arguments define the extent of the ruler, and max_size is the largest possible number that will be displayed. Position defines the initial position of the pointer indicator within the ruler.
A vertical ruler can span an 800 pixel
wide window thus
vruler.set_range(0, 800, 0, 800) |
The markings displayed on the ruler will
be from 0 to 800, with a number for every 100 pixels. If instead we wanted
the ruler to range from 7 to 16, we would code
vruler.set_range(7, 16, 0, 20) |
The indicator on the ruler is a small
triangular mark that indicates the position of the pointer relative to
the ruler. If the ruler is used to follow the mouse pointer, the motion_notify_event
signal should be connected to the motion_notify_event
method of the ruler. We need to setup a motion_notify_event
callback for the area and
use connect_object() to get
the ruler to emit a motion_notify_signal:
def motion_notify(ruler, event):
return ruler.emit("motion_notify_event", event)
area.connect_object("motion_notify_event", motion_notify, ruler)
|
The rulers.py example program creates a drawing area with a horizontal ruler above it and a vertical ruler to the left of it. The size of the drawing area is 600 pixels wide by 400 pixels high. The horizontal ruler spans from 7 to 13 with a mark every 100 pixels, while the vertical ruler spans from 0 to 400 with a mark every 100 pixels. Placement of the drawing area and the rulers is done using a table. Figure 9.7 illustrates the result:

1 #!/usr/bin/env python
2
3 # example rulers.py
4
5 import gtk
6 import GDK
7
8 class RulersExample:
9 XSIZE = 600
10 YSIZE = 400
11
12 # This routine gets control when the close button is clicked
13 def close_application(self, widget, event, data=None):
14 gtk.mainquit()
15 return gtk.FALSE
16
17 def __init__(self):
18 window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
19 window.connect("delete_event", self.close_application)
20 window.set_border_width(10)
21
22 # Create a table for placing the ruler and the drawing area
23 table = gtk.GtkTable(3, 2, gtk.FALSE)
24 window.add(table)
25
26 area = gtk.GtkDrawingArea()
27 area.size(self.XSIZE, self.YSIZE)
28 table.attach(area, 1, 2, 1, 2,
29 gtk.EXPAND|gtk.FILL, gtk.FILL, 0, 0 )
30 area.set_events(GDK.POINTER_MOTION_MASK |
31 GDK.POINTER_MOTION_HINT_MASK )
32
33 # The horizontal ruler goes on top. As the mouse moves across the
34 # drawing area, a motion_notify_event is passed to the
35 # appropriate event handler for the ruler.
36 hrule = gtk.GtkHRuler()
37 hrule.set_metric(gtk.PIXELS)
38 hrule.set_range(7, 13, 0, 20)
39 def motion_notify(ruler, event):
40 return ruler.emit("motion_notify_event", event)
41 area.connect_object("motion_notify_event", motion_notify, hrule)
42 table.attach(hrule, 1, 2, 0, 1,
43 gtk.EXPAND|gtk.SHRINK|gtk.FILL, gtk.FILL, 0, 0 )
44
45 # The vertical ruler goes on the left. As the mouse moves across
46 # the drawing area, a motion_notify_event is passed to the
47 # appropriate event handler for the ruler.
48 vrule = gtk.GtkVRuler()
49 vrule.set_metric(gtk.PIXELS)
50 vrule.set_range(0, self.YSIZE, 10, self.YSIZE)
51 area.connect_object("motion_notify_event", motion_notify, vrule)
52 table.attach(vrule, 0, 1, 1, 2,
53 gtk.FILL, gtk.EXPAND|gtk.SHRINK|gtk.FILL, 0, 0 )
54
55 # Now show everything
56 area.show()
57 hrule.show()
58 vrule.show()
59 table.show()
60 window.show()
61
62 def main():
63 gtk.mainloop()
64 return 0
65
66 if __name__ == "__main__":
67 RulersExample()
68 main()
|
Lines 41 and 51 connect the motion_notify() callback to the area but passing hrule in line 41 and vrule in line 55 as user data. The motion_notify() callback will be called twice each time the mouse moves - once with hrule and once with vrule.
| <<< Previous | Home | Next >>> |
| Pixmaps | Up | Statusbars |